Pandas data frame from dictionary returns values as tuple

Viewed 198

Hey guys If someone could give me an explanation I would be very grateful. This is a code:

#Dict- 
hours_year={'2018': 900 , '2019':456} 
# Making a df

df=pd.DataFrame({'Year': hours_year.keys(), 'Hours': hours_year.values()}) 
df
#Output

            Year    Hours
0   (2018, 2019)    (900, 456)

1   (2018, 2019)    (900, 456)

My question is- Why tuple? On other pc, same in jupyter it return normal df. Could it be something with version of python that i'am using?? Just guessing.. Thanks for any help

2 Answers

Founded here:

pd.DataFrame(hours_year, index=['i',])

Else, you should try from_dict function, but your data need to be list.

df = pd.DataFrame.from_dict(hours_year, columns=['Years', 'Hours'])

Another solution but you need to change your data again :

hours_year={'2018': [900,] , '2019':[456,]}
pd.DataFrame(hours_year)

I solved it df=pd.DataFrame({'Year': list(hours_year.keys()), 'Hours': list(hours_year.values())}) Just putting a list IN front of value. And the original code works. Still not sure why on one pc must stand list and on another not.

Related