问题:原来数据是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
# (index,column name,value)
df.insert(index,'columnname',list)
df.insert(0,'add',1)
删除pandas DataFrame的某一/几列:
方法一:直接del DF[‘column-name’]
方法二:采用drop方法,有下面三种等价的表达式:
1. DF= DF.drop('column_name', 1);
2. DF.drop('column_name',axis=1, inplace=True)
3. DF.drop([DF.columns[[0,1, 3]]], axis=1,inplace=True) # Note: zero indexed
注意:凡是会对原数组作出修改并返回一个新数组的,往往都有一个 inplace可选参数。如果手动设定为True(默认为False),那么原数组直接就被替换。也就是说,采用inplace=True之后,原数组名(如2和3情况所示)对应的内存值直接改变;而采用inplace=False之后,原数组名对应的内存值并不改变,需要将新的结果赋给一个新的数组或者覆盖原数组的内存位置(如1情况所示)。