map() is for Series (i.e. single columns) and operates on one cell at a time,
apply() is for DataFrame, and operates on a whole row at a time.
pdd=pd.DataFrame([[4,9],]*3, columns=['A','B'])
pdd
Out[8]:
A B
0 4 9
1 4 9
2 4 9
>>> df.apply(np.sum, axis=0)# 逐行相加,即求**列和**
A 12
B 27
dtype: int64
>>> df.apply(np.sum, axis=1)
0 13
1 13
2 13
dtype: int64
pdd
Out[8]:
A B
0 4 9
1 4 9
2 4 9
pdd.apply(lambda x:[1,2], axis=0) #axis=0表示逐行进行
Out[9]:
A [1, 2]
B [1, 2]
dtype: object
pdd.apply(lambda x:[1,2],axis=1)#axis=1表示逐列进行
Out[10]:
0 [1, 2]
1 [1, 2]
2 [1, 2]
dtype: object
**Passing result_type=’expand’ will expand list-like results to columns of a Dataframe**
pdd.apply(lambda x:[1,2],axis=1,result_type='expand') #axis=1表示逐列进行
Out[12]:
0 1
0 1 2
1 1 2
2 1 2