小程序容器助力企业在金融与物联网领域实现高效合规运营,带来的新机遇与挑战如何管理?
655
2022-11-04
Pandas列中的字典/列表拆分为单独的列
[1] dfStation ID Pollutants8809 {"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 c0 46 3 121 36 5 82 NaN 2 73 NaN NaN 114 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 c0 8809 46 3 121 8810 36 5 82 8811 NaN 2 73 8812 NaN NaN 114 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 c0 8809 46 3 121 8810 36 5 82 8811 NaN 2 73 8812 NaN NaN 114 8813 82 NaN 15
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~