Pankaj
Pankaj

Reputation: 517

Making dictionary of dataframes from dictionary of dictionaries in pandas

I have a nested dictionary:

    Night_interval={
    '2010': {
      Timestamp('2010-07-01 00:00:00'): 29,
      Timestamp('2010-07-02 00:00:00'): 28,
      Timestamp('2010-07-03 00:00:00'): 28,
      Timestamp('2010-07-04 00:00:00'): 29,
      Timestamp('2010-07-05 00:00:00'): 28
            },
    '2011': {
      Timestamp('2010-07-01 00:00:00'): 29,
      Timestamp('2010-07-02 00:00:00'): 28,
      Timestamp('2010-07-03 00:00:00'): 28,
      Timestamp('2010-07-04 00:00:00'): 29,
      Timestamp('2010-07-05 00:00:00'): 28
            },
    '2012': {
      Timestamp('2010-07-01 00:00:00'): 29,
      Timestamp('2010-07-02 00:00:00'): 28,
      Timestamp('2010-07-03 00:00:00'): 28,
      Timestamp('2010-07-04 00:00:00'): 29,
      Timestamp('2010-07-05 00:00:00'): 28
            }
  }

Using this dictionary, I want to create a dictionary of dataframes associated with each key, i.e., 2010, 2011 and 2012. I also want to put the Timestamp as the indices in each of the dataframes. I tried writing following code:

Years = ['2010','2011','2012']
for key in Years:
    df_interval[key] = pd.DataFrame(Night_interval[key]) 

However, I am getting this error: ValueError: If using all scalar values, you must pass an index

I am not able to find where I am doing wrong. I would appreciate any help.

Upvotes: 1

Views: 556

Answers (1)

jezrael
jezrael

Reputation: 862511

If need dict use DataFrame constructor with column name:

df_interval = {}
Years = ['2010','2011','2012']
for key in Years:
    df_interval[key] = pd.DataFrame({key:Night_interval[key]})

print (df_interval['2012'])
            2012
2010-07-01    29
2010-07-02    28
2010-07-03    28
2010-07-04    29
2010-07-05    28

df_interval = {}
Years = ['2010','2011','2012']
for key in Years:
    df_interval[key] = pd.DataFrame({'a':Night_interval[key]})

print (df_interval['2012'])
             a
2010-07-01  29
2010-07-02  28
2010-07-03  28
2010-07-04  29
2010-07-05  28

Or if only one column is possible create dict of Series:

df_interval = {}
Years = ['2010','2011','2012']
for key in Years:
    df_interval[key] = pd.Series(Night_interval[key], name=key)

print (df_interval['2012'])
2010-07-01    29
2010-07-02    28
2010-07-03    28
2010-07-04    29
2010-07-05    28
Name: 2012, dtype: int64

Upvotes: 1

Related Questions