Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling dict with NA values to allow conversion to pandas dataframe

I have a dict that holds computed values on different time lags, which means they start on different dates. For instance, the data I have may look like the following:

Date      col1    col2    col3    col4    col5
01-01-15  5       12      1      -15      10
01-02-15  7       0       9       11      7
01-03-15          6       1       2       18
01-04-15          9       8       10
01-05-15         -4               7
01-06-15         -11             -1
01-07-15          6               

Where each header is the key, and each column of values is the value for each key (I'm using a defaultdict(list) for this). When I try to run pd.DataFrame.from_dict(d) I understandably get an error stating that all arrays must be the same length. Is there an easy/trivial way to fill or pad the numbers so that the output would end up being the following dataframe?

Date      col1    col2    col3    col4    col5
01-01-15  5       12      1      -15      10
01-02-15  7       0       9       11      7
01-03-15  NaN     6       1       2       18
01-04-15  NaN     9       8       10      NaN
01-05-15  NaN    -4       NaN     7       NaN
01-06-15  NaN    -11      NaN    -1       NaN
01-07-15  NaN     6       NaN     NaN     NaN

Or will I have to do this manually with each list?

Here is the code to recreate the dictionary:

import pandas as pd
from collections import defaultdict

d = defaultdict(list)
d["Date"].extend([
    "01-01-15", 
    "01-02-15", 
    "01-03-15", 
    "01-04-15", 
    "01-05-15",
    "01-06-15",
    "01-07-15"
]
d["col1"].extend([5, 7])
d["col2"].extend([12, 0, 6, 9, -4, -11, 6])
d["col3"].extend([1, 9, 1, 8])
d["col4"].extend([-15, 11, 2, 10, 7, -1])
d["col5"].extend([10, 7, 18])
like image 309
weskpga Avatar asked Jul 18 '16 21:07

weskpga


People also ask

How do I convert a dictionary to a DataFrame pandas?

You can convert a dictionary to Pandas Dataframe using df = pd. DataFrame. from_dict(my_dict) statement.

Can we create DataFrame from dictionary?

Method 1: Create DataFrame from Dictionary using default Constructor of pandas. Dataframe class. Method 2: Create DataFrame from Dictionary with user-defined indexes. Method 3: Create DataFrame from simple dictionary i.e dictionary with key and simple value like integer or string value.


2 Answers

Another option is to use from_dict with orient='index' and then take the tranpose:

my_dict = {'a' : [1, 2, 3, 4, 5], 'b': [1, 2, 3]}
df = pd.DataFrame.from_dict(my_dict, orient='index').T

Note that you could run into problems with dtype if your columns have different types, e.g. floats in one column, strings in another.

Resulting output:

     a    b
0  1.0  1.0
1  2.0  2.0
2  3.0  3.0
3  4.0  NaN
4  5.0  NaN
like image 126
root Avatar answered Oct 15 '22 16:10

root


#dictionary of different lengths...
my_dict = {'a' : [1, 2, 3, 4, 5], 'b': [1, 2, 3]}
pd.DataFrame(dict([(col_name,pd.Series(values)) for col_name,values in my_dict.items() ]))

Output -

   a    b
0  1  1.0
1  2  2.0
2  3  3.0
3  4  NaN
4  5  NaN
like image 27
hashcode55 Avatar answered Oct 15 '22 14:10

hashcode55