当前位置 : 主页 > 编程语言 > java >

Pandas列中的字典/列表拆分为单独的列

来源:互联网 收集:自由互联 发布时间:2022-07-04
[ 1 ] df Station ID Pollutants 8809 { "a" : "46" , "b" : "3" , "c" : "12" } 8810 { "a" : "36" , "b" : "5" , "c" : "8" } 8811 { "b" : "2" , "c" : "7" } 8812 { "c" : "11" } 8813 { "a" : "82" , "c" : "15" } 1. step 1: convert the Pollutants co
[1] df
Station ID Pollutants
8809 {"a": "46", "b": "3", "c": "12"}
8810 {"a": "36", "b": "5", "c": "8"}
8811 {"b": "2", "c": "7"}
8812 {"c": "11"}
8813 {"a": "82", "c": "15"}

1. step 1: convert the Pollutants column to Pandas dataframe series

df_pol_ps = data_df['Pollutants'].apply(pd.Series)

df_pol_ps:
a b c
0 46 3 12
1 36 5 8
2 NaN 2 7
3 NaN NaN 11
4 82 NaN 15

step 2: concat columns a, b, c and drop/remove the Pollutants

df_final = pd.concat([df, df_pol_ps], axis = 1).drop('Pollutants', axis = 1)

df_final:
StationID a b c
0 8809 46 3 12
1 8810 36 5 8
2 8811 NaN 2 7
3 8812 NaN NaN 11
4 8813 82 NaN 15

Method 2:一步搞定

df_final = pd.concat([df, df['Pollutants'].apply(pd.Series)], axis = 1).drop('Pollutants', axis = 1)

df_final:
StationID a b c
0 8809 46 3 12
1 8810 36 5 8
2 8811 NaN 2 7
3 8812 NaN NaN 11
4 8813 82 NaN 15


上一篇:vue 的axios 文件上传
下一篇:没有了
网友评论