我有pandas df,其中一列包含不同大小的列表。虽然列表的最大大小可以为10。我的要求是转换这些列表并将其添加到列中。我的数据样本看起来像
prod_1 prod_2 subs_number
34007 [3330193, 349723, 3097333, 8145148] 4
34492 [7554951, 349723, 3097340, 350293, 3097333, 22... 8
126063 [350293, 7901614, 7901597, 349723, 7901731, 30... 7
126290 [3097340, 7901614, 7900213, 350293, 7901597] 5
222338 [7554913, 7554951, 3214604, 34492, 8145148] 5
我想要以下结果:
prod_1 subs1 subs2 subs3 subs4 subs5 subs6 subs7
34007 3330193 349723 3097333 8145148
126063 350293 7901614 7901597 349723 7901731
在大熊猫中可行吗?请帮忙
答案 0 :(得分:2)
无论列表多长时间都应该工作
示例:
df = pd.DataFrame([[34007, [3330193, 349723, 3097333,8145148]], [126063, [350293,7901614,7901597,349723,7901731]], [10, [1,2,3,4,5,6,7,8,9,10]]], columns=['prod_1', 'prod_2'])
样本输出:
prod_1 prod_2
0 34007 [3330193, 349723, 3097333, 8145148]
1 126063 [350293, 7901614, 7901597, 349723, 7901731]
2 10 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
创建愿望数据框:
pd.DataFrame(df['prod_2'].values.tolist()).add_prefix('subs').join(df['prod_1'])
答案 1 :(得分:1)
未经测试的产品应与pd.Series
一起使用
df2 = df2.prod_2.apply(pd.Series)
df2.columns = ['subs1', 'subs2','subs3', 'subs4','subs5', 'subs6','subs7']
print(df2)
答案 2 :(得分:1)
希望这行得通!
org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [HikariDataSource (HikariPool-2)] with key 'whgDataSource'; nested exception is javax.management.InstanceAlreadyExistsException: whgDataSourceConfig:name=whgDataSource,type=HikariDataSource