How can I create a dataframe from a list of dictionaries that contain list of rows for each key? Please check example below:
>>> import pandas as pd
>>> rec_set1 = {'col1': [1,2,3], 'col2': [5,3,4], 'col3': ['x','y','z']}
>>> rec_set2 = {'col1': [5,6,7], 'col2': [-4,6,2], 'col3': ['p','q','r']}
>>> rec_set_all = [rec_set1, rec_set2]
>>> df = pd.DataFrame.from_records(rec_set1)
>>> df
col1 col2 col3
0 1 5 x
1 2 3 y
2 3 4 z
All good so far.
Now I try to append rec_set2 and this is what happens:
>>> df = df.append(rec_set2, ignore_index=True)
>>> df
col1 col2 col3
0 1 5 x
1 2 3 y
2 3 4 z
3 [5, 6, 7] [-4, 6, 2] [p, q, r]
Not what I was expecting. What append function should I use ?
And rather than doing it in a loop, is there a simple one-line way to create the entire dataframe from
rec_set_all?
pd.concat([pd.DataFrame(rec_set1), pd.DataFrame(rec_set2)])?df = df.append(pd.DataFrame(rec_set2), ignore_index=True)work? as in you just forgot to turn the other dictionary into a dataframe?.append()?