-1

This is my structure :

data = [{'dev_name': 'some_name',
'dev_connects': ['cn1', 'cn2', 'cn3', 'cn4'], 
'dev_type': 'some_type', 
'dev_features': ['ft 1', 'ft 2']},

{'dev_name': 'some_name2', 
'dev_connects': ['cn1', 'cn2', 'cn3', 'cn4'], 
'dev_type': 'some_type2', 
'dev_features': ['ft 1', 'ft 2']}]

So as you can see from my description , its an array of dicts. I need to be able to create a dataframe that will read each object and columnize every key, including what is inside the arrays data[i]['dev_connects'] and data[i]['dev_features'] for every object :

dev_name connect1 connect2 connect3 connect4 dev_type feature1 feature2
some_name cn1 cn2 cn3 cn4 some_type ft1 ft2
some_name2 cn1 cn2 cn3 cn4 some_type2 ft1 ft2
Omar Aziz
  • 19
  • 4
  • Does this answer your question? [Split a Pandas column of lists into multiple columns](https://stackoverflow.com/questions/35491274/split-a-pandas-column-of-lists-into-multiple-columns) – Ynjxsjmh Aug 05 '22 at 07:14

1 Answers1

1

I was able to accomplish this by breaking up your DataFrames into two separate (via apply(pd.Series) and then merging them back together. See the final code below.

df_base = pd.DataFrame(data)[["dev_name", "dev_type"]]
df_connects = pd.DataFrame(data).set_index("dev_name")["dev_connects"].apply(pd.Series).add_prefix("connect")
df_features = pd.DataFrame(data).set_index("dev_name")["dev_features"].apply(pd.Series).add_prefix("feature")

df_final = pd.merge(df_base, df_connects, on="dev_name").merge(df_features, on="dev_name")

This leaves you with the following final result.

dev_name dev_type connect0 connect1 connect2 connect3 feature0 feature1
0 some_name some_type cn1 cn2 cn3 cn4 ft 1 ft 2
1 some_name2 some_type2 cn1 cn2 cn3 cn4 ft 1 ft 2
mattdonders
  • 1,328
  • 1
  • 19
  • 42