gpt4 book ai didi

python - 使用 Pandas 拆分几列

转载 作者:太空宇宙 更新时间:2023-11-04 00:51:12 26 4
gpt4 key购买 nike

我想将字符串分成几列。例如,我想从下面数据框中的 col2、col3 和 col5 中选择一些信息(但实际上我有一百多个列可以这样做)。

d = pd.DataFrame({
'col1' : ['USA', 'AGN'],
'col2' : ['0|0:0.014:0.986,0.013,0', '1|0:0.02:1.936,0.023,1'],
'col3' : ['1|0:0.024:0.9,0.01345,2', '0|2:0.213:0.92,0.1,2'],
'col4' : ['done', 'done'],
'col5' : ['2|0:0.02:1.936,0.023,1', '1|0:0.024:0.9,0.01345,2']
})

col1 col2 col3 col4 .....
0 USA 0|0:0.014:0.986,0.013,0 1|0:0.024:0.9,0.01345,2 done .....
1 AGN 1|0:0.02:1.936,0.023,1 0|2:0.213:0.92,0.1,2 done .....

我只需要那个长字符串的前 3 个标记。然后我希望我可以从下面的结果中看到。

col1 col2  col3  col4  col5  ....
USA 0|0 1|0 done 2|0 ....
AGN 1|0 0|2 done 1|0 ....

有什么提示吗?

最佳答案

如果我正确理解你的问题,你可以这样做:

In [254]: d.replace(r':.*', '', regex=True)
Out[254]:
col1 col2 col3 col4 col5
0 USA 0|0 1|0 done 2|0
1 AGN 1|0 0|2 done 1|0

关于python - 使用 Pandas 拆分几列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37033352/

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