duc*_*man 2 python string dataframe python-3.x pandas
我有一个 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
Run Code Online (Sandbox Code Playgroud)
哪里want1来want3的我需要什么。
您可以pd.Series.str.split为此使用:
df[['want1', 'want2', 'want3']] = df['variable'].str.split(' - ', expand=True)
Run Code Online (Sandbox Code Playgroud)