doubts in the creation of dataframe pandas

Asked

Viewed 43 times

0

I have a question in creating a dataframe as an example

dicionario = {'pais': 'brasil', 'capital': 'Brasilia', 'climate': 'tropical'}

pd.Dataframe(dictionary)

back index error

but put some value on the list dicionario = {'pais': ['brasil'], 'capital': 'Brasilia', 'climate': 'tropical'}

pd.Dataframe(dictionary)

Back a perfect dataframe pandas with index and columns

2 answers

2

The solution is simple

use:

df = pd.DataFrame([dicionario])

However, this is because there is only one line. Normally you would create Dataframe as the example below:

>>> dicionario = {'pais': ['brasil', 'argentina', 'australia'],
                  'capital': ['brasilia', 'buenos aires', 'canberra'],
                  'clima': ['tropical', 'temperado', 'tropical']}
>>> df = pd.DataFrame(dicionario)
>>> df
        pais       capital      clima
0     brasil      brasilia   tropical
1  argentina  buenos aires  temperado
2  australia      canberra   tropical
>>>

I hope it helps

1

This problem of yours can be done in two ways:

import pandas as pd

dicionario = {'pais': 'brasil', 'capital': 'brasilia', 'clima': 'tropical'}

pd.DataFrame([dicionario])

print(dicionario)

Alternatively:

import pandas as pd

dicionario = {'pais': 'brasil', 'capital': 'brasilia', 'clima': 'tropical'}

pd.DataFrame(dicionario, index=[0])

print(dicionario)

Browser other questions tagged

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