Nicola Lepetit
Nicola Lepetit

Reputation: 793

Creating Pandas dataframe from one dictionary and a ditionary of array

I have the following array and dict:

periods = ['2018-06', '2018-09', '2018-12']
info = {
   'income' : [22381, 15733, 1032],
   'expense' : [8192, 8768, 8888],
   'bonus' : [0, 500, 0]
}

And I want to create a dataframe with one row for each date and entry, with the value. Something like:

date      fact       value
'2018-06' 'income'   22381
'2018-09' 'income'   15733
'2018-12' 'income'    1032
'2018-06' 'expense'   8192
'2018-09' 'expense'   8768
'2018-12' 'expense'   8888
'2018-06' 'bonus'        0
'2018-09' 'bonus'      500
'2018-12' 'bonus'        0

Is there a way to do it without iterating on each element of the dictionary ?

Upvotes: 1

Views: 13

Answers (1)

mozway
mozway

Reputation: 260380

You can use a stack approach:

df = (pd.DataFrame(info, index=periods)
        .stack().rename_axis(['date', 'fact'])
        .reset_index(name='value')
     )

output:

      date     fact  value
0  2018-06   income  22381
1  2018-06  expense   8192
2  2018-06    bonus      0
3  2018-09   income  15733
4  2018-09  expense   8768
5  2018-09    bonus    500
6  2018-12   income   1032
7  2018-12  expense   8888
8  2018-12    bonus      0

Or, if order matters, use melt:

df = (pd.DataFrame(info, index=periods)
        .rename_axis('date').reset_index()
        .melt('date', var_name='fact', value_name='value')
     )

output:

      date     fact  value
0  2018-06   income  22381
1  2018-09   income  15733
2  2018-12   income   1032
3  2018-06  expense   8192
4  2018-09  expense   8768
5  2018-12  expense   8888
6  2018-06    bonus      0
7  2018-09    bonus    500
8  2018-12    bonus      0

Upvotes: 1

Related Questions