Search code examples
pythonpandasdataframeseries

How to convert a pandas series of objects into a dataframe where each item becomes a column and the values in the rows


I am stuck at this: I have a Series with the following structure Name: sn dtype: object

0 { key1: value1, key2: value2, key3: value3} 
1 { key1: value4, key2: value5, key3: value6} 
2 { key1: value7, key2: value8, key3: value9} 
3 { key1: value10, key2: value11, key3: value12} 

I want to transform it into a DataFrame

   <key1>     <key2>    <key3>  # columns
0  <value1>   <value2>  <value3>
1  <value4>   <value5>  <value6>
2  <value7>   <value8>  <value9>
3  <value10>   <value11>  <value12>

I have tried using:

cols = ['key1','key2','key3']
df = pd.DataFrame(data=sn_sites['sn'], index=cols)

I was also able to iterate over the series and print item keys I need.

for index, value in sn.items():
  print (f{value['key1']}, {value['key3']) # works 
  df['column1'] = value['key1'] # doesn't work

But I still need a dataframe as output so I can progress on building my structure with columns from other dataframes and values like this inside series. Is there an simple way to do it?


Solution

  • IIUC, this should work:

    df = pd.DataFrame.from_records(sn)