How to copy a specific data frame line to another data frame

Asked

Viewed 1,702 times

0

I have following Data Frame:

data = {
'País': ['Bélgica', 'Índia', 'Brasil','Índia','Bélgica','Bélgica','Russia','Brasil'],
'Capital': ['Bruxelas1', 'Nova Delhi', 'Brasília', 'Nova Delhi','Bruxelas2','Bruxelas3','Moscou','Brasília'],
'Continente': ['Europa', 'Asia', 'America do Sul', 'Asia','Europa','Europa','Europa','America do Sul'],
'População': [123465, 456789, 987654, 456789,123465,123465,350000000,987654]}
df = pd.DataFrame(data, columns=['País','Capital','Continente','População'])

df1 = df.loc[0]

df1 is a series(pandas.core.series.Series). . I need df1 to be data frame

1 answer

1

Hello,

If you are trying to make a cutout of dataframe by indices, use df.iloc[linha, coluna]. The loc is used when you want to crop with a column name or a row value.

To convert a pandas.Series in a pandas.DataFrame, just call the function pandas.Series.to_frame() as explained here in the documentation: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.to_frame.html

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.