2

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

Epsi95
  • 8,832
  • 1
  • 16
  • 34
Sasa Arsic
  • 21
  • 2

2 Answers2

0

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)
Rallyx
  • 38
  • 8
  • With that code a got: ValueError: cannot use columns parameter with orient='columns', so I add orient, df = pd.DataFrame.from_dict(hours_year,orient='index',columns=['Years', 'Hours']), then I got this ValueError: Shape of passed values is (2, 1), indices imply (2, 2). I solved the problem whit this code details = { 'Year' :['2018','2019'], 'Hours' : [3702, 9818] } # creating a Dataframe object df = pd.DataFrame(details) I just do not understand why it won't work with something that I used soo many times. – Sasa Arsic Mar 03 '21 at 08:59
0

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.

Sasa Arsic
  • 21
  • 2