apply:可作用与Series,也可以作用DataFrame
作用于Series
*data
CabinFareAge
0Acd7.829234.5
1Vafe7.000047.0
2Cfwrw9.687562.0
3Rfdf8.662527.0
4Mfdf12.287522.0
*data['cabin']=data['Cabin'].apply(lambda x:x[0])
*data['cabin']
0 A
1 V
2 C
3 R
4 M
作用与DataFrame
*data[['Fare','Age']].apply(lambda x:x.max()-x.min())
Fare 5.2875
Age 40.0000
map只可作用于Series
*data['cabin1'] = data['Cabin'].map(lambda x:x[0])
*data['cabin1']
0 A
1 V
2 C
3 R
4 M
*data[['Fare','Age']].map(lambda x:x.max()-x.min())
AttributeError: 'DataFrame' object has no attribute 'map'
#还可以用作替换
*x = pd.Series([0,1,2], index=['one', 'two', 'three'])
*y = pd.Series(['foo', 'bar', 'baz'], index=[0,1,2])
*x.map(y) #x的值与y的index 值相同,可将替换,不同,以NaN填充
one foo
two bar
three baz
dtype: object
#作用于合并
*x.map('you have {} pen'.format,na_action=None) #na_action 对NaN填充,'ignore'忽视NaN不填充
one you have 0 pen
two you have 1 pen
three you have 2 pen
applymap 部分行、列,对所有元素进行操作。
操作对象可以是DataFrame 或者 Series