python学习--列替换问题

问题:原来数据是score列包含三列,修整列,并把列分开并替换原列

代码:

p=pd.DataFrame({'gender':[' M',' M ',' F ','  M','F'],
               'name':['jack','tom','marry','zack','heheda'],
               'socre':['90,90,90','89,88,87','90,90,90','78,78,78','60,60,60']})
print(p)
    gender    name     socre
0      M    jack  90,90,90
1     M      tom  89,88,87
2     F    marry  90,90,90
3      M    zack  78,78,78
4      F  heheda  60,60,60
p['gender']=p['gender'].str.strip()
print(p)
  gender    name     socre
0      M    jack  90,90,90
1      M     tom  89,88,87
2      F   marry  90,90,90
3      M    zack  78,78,78
4      F  heheda  60,60,60
p['name']=p['name'].str.upper()
print(p)
  gender    name     socre
0      M    JACK  90,90,90
1      M     TOM  89,88,87
2      F   MARRY  90,90,90
3      M    ZACK  78,78,78
4      F  HEHEDA  60,60,60
p['english']=p['socre'].str.split(',').str.get(0)
p['math']=p['socre'].str.split(',').str.get(1)
p['art']=p['socre'].str.split(',').str.get(2)
p.drop('socre',axis=1, inplace=True)
print(p)
   gender    name english math art
0      M    JACK      90   90  90
1      M     TOM      89   88  87
2      F   MARRY      90   90  90
3      M    ZACK      78   78  78
4      F  HEHEDA      60   60  60
posted @ 2019-04-24 12:50  ttkk89  阅读(1730)  评论(0编辑  收藏  举报