user2529426
user2529426

Reputation: 31

Creating a pandas.DataFrame from a dict

I'm new to using pandas and I'm trying to make a dataframe with historical weather data.

The keys are the day of the year (ex. Jan 1) and the values are lists of temperatures from those days over several years.

I want to make a dataframe that is formatted like this:

... Jan1   Jan2   Jan3 etc
1   temp   temp   temp etc
2   temp   temp   temp etc
etc etc    etc    etc

I've managed to make a dataframe with my dictionary with

df = pandas.DataFrame(weather) 

but I end up with 1 row and a ton of columns.

I've checked the documentation for DataFrame and DataFrame.from_dict, but neither were very extensive nor provided many examples.

Upvotes: 1

Views: 117

Answers (1)

unutbu
unutbu

Reputation: 880359

Given that "the keys are the day of the year... and the values are lists of temperatures", your method of construction should work. For example,

In [12]: weather = {'Jan 1':[1,2], 'Jan 2':[3,4]}

In [13]: df = pd.DataFrame(weather) 

In [14]: df
Out[14]: 
   Jan 1  Jan 2
0      1      3
1      2      4

Upvotes: 1

Related Questions