pandas 将一列拆分成多列

df = pd.DataFrame({'name':['jack','tom','Marry','zack','heheda'],
                  'gender':['M ','M','   F','  M ','  F'],
                  'score':['90-92-89','89-78-88','90-92-95','78-88-76','60-60-67']})
print(df)

gender    name     score
0     M     jack  90-92-89
1      M     tom  89-78-88
2      F   Marry  90-92-95
3     M     zack  78-88-76
4      F  heheda  60-60-67

df['name'] = df['name'].str.capitalize()
df['gender'] = df['gender'].str.strip()

#拆分列
df['math'] = df['score'].apply(lambda x: x.split('-')[0])  
df['english'] = df['score'].apply(lambda x: x.split('-')[1]) 
df['art'] = df['score'].apply(lambda x: x.split('-')[2]) 

print(df)

gender    name     score math english art
0      M    Jack  90-92-89   90      92  89
1      M     Tom  89-78-88   89      78  88
2      F   Marry  90-92-95   90      92  95
3      M    Zack  78-88-76   78      88  76
4      F  Heheda  60-60-67   60      60  67
#整理:

你可能感兴趣的:(pandas 将一列拆分成多列)