将字段Key作为新列名,value作为列值生成多列。
import pandas as pd
data1 = {
"a": [1, 2, 3],
"b": [{'X': 10, 'Y': 20}, {'X': 50, 'Y': 100}, {'X': 33, 'Y': 44}],
"c": [7, 8, 9]
}
df = pd.DataFrame(data1)
print(df)
print("==============")
list_temp = df['b'].values.tolist()
temp = pd.DataFrame(list_temp)
df['X'] = temp['X']
df['Y'] = temp['Y']
df = df.drop(['b'], axis=1)
print(df)
结果
a b c
0 1 {'X': 10, 'Y': 20} 7
1 2 {'X': 50, 'Y': 100} 8
2 3 {'X': 33, 'Y': 44} 9
==============
a c X Y
0 1 7 10 20
1 2 8 50 100
2 3 9 33 44