[英]How to split a column into three columns?
I have a dataset of a soccer league:我有一个足球联赛的数据集:
df.Game.head()
0 Man. City @Cardiff City
1 Southampton @Liverpool
2 Tottenham @Brighton
3 Chelsea @West Ham
4 Wolves @Man United
The symbol '@' before the team name indicates home team.队名前的符号“@”表示主队。 I want to split the column into three separate columns:
我想将该列拆分为三个单独的列:
I have tried the following code:我尝试了以下代码:
df[['team_1', 'team_2']] = df.Game.str.split(' @', expand = True)
df['home'] = df.Game.str.split(' @', expand = True)[1]
Is there any better way to do that?有没有更好的方法来做到这一点? Maybe a single-line code?
也许是单行代码? Thank you!
谢谢!
You do not need to split
twice您不需要
split
两次
df[['team_1', 'team_2']] = df.Game.str.split(' @', expand = True)
df['home'] = df['team 2']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.