Home > Software design >  How to convert a pandas series of objects into a dataframe where each item becomes a column and the
How to convert a pandas series of objects into a dataframe where each item becomes a column and the

Time:08-20

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?

CodePudding user response:

IIUC, this should work:

df = pd.DataFrame.from_records(sn)

CodePudding user response:

Your Series contains a dict in each row. Pandas can construct a DataFrame from structured input data like your Series using pd.DataFrame.from_records.

import pandas as pd

raw = [
    {"key1": 1, "key2": 2, "key3": 3},
    {"key1": 4, "key2": 5, "key3": 6},
    {"key1": 7, "key2": 8, "key3": 9},
]

# Each element is a dict
d_series = pd.Series(raw)

df = pd.DataFrame.from_records(d_series)

You can also unpack the Series right into the DataFrame constructor.

pd.DataFrame(*d_series)
  • Related