简体   繁体   English

如何将一列拆分为三列?

[英]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:我想将该列拆分为三个单独的列:

  1. team_1: Team one team_1:第一队
  2. team_2: Team two team_2:二队
  3. home: The home team (which has @ before the name and always comes at the second) home:主队(名字前有@,总是排在第二位)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM