gpt4 book ai didi

python - Pandas df 将值从一列拆分为自己的列

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:08 25 4
gpt4 key购买 nike

如何使用 Pandas 和 Python 执行此操作?我正在使用 Jupyter Notebook。

我有一个这样的数据框:

    col
0 2017 / something
1 $5.91 (× 1)
2 Premium
3 2017 / anotherthing
4 $16.0 (× 1)
5 Business

我想将其中的值拆分成它们自己的列,如下所示:

    col                    revenue        plan
0 2017 / something $5.91 (× 1) Premium
1 2017 / anotherthing $16.0 (× 1) Business

然后从收入列中删除括号值和美元符号,这样我就可以结束了:

    col                    revenue     plan
0 2017 / something 5.91 Premium
1 2017 / anotherthing 16.0 Business

最佳答案

In [113]: (df[['col']].replace(r'\s*\([^\)]*\)', '', regex=True)
...: .set_index(np.arange(len(df))//3)
...: .set_index(np.arange(len(df))%3, append=True)['col']
...: .unstack())
...:
Out[113]:
0 1 2
0 2017 / something $5.91 Premium
1 2017 / anotherthing $16.0 Business

关于python - Pandas df 将值从一列拆分为自己的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49579729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com