gpt4 book ai didi

python - 从另一列的子字符串创建列

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

我有一个 Pandas 数据框对象。我想从现有列的子字符串创建新列。我的数据如下所示:

    Date        variable                 want1          want2   want3
0 02-01-08 Australia - Sydney - A Australia Sydney A
1 03-01-08 Australia - Sydney - A Australia Sydney A
2 04-01-08 Australia - Sydney - A Australia Sydney A
3 05-01-08 Canada - Toronto - B Canada Toronto B
4 06-01-08 Canada - Toronto - B Canada Toronto B

want1want3 是我需要的。

最佳答案

为此,您可以使用 pd.Series.str.split:

df[['want1', 'want2', 'want3']] = df['variable'].str.split(' - ', expand=True)

关于python - 从另一列的子字符串创建列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49807976/

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