pandas的map函數與apply函數的區別

import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(4,3),columns=list("ABC"),index=[1,2,3,4]) #apply函數對DataFrame和Series的一列作總體運算
df.apply(lambda x:x.max()-x.min()) # ============================================================================= # A 2.862952 # B 2.463625 # C 3.524467 # =============================================================================

#applymap做用於DataFrame的每個元素
df.applymap(lambda x:int(x)) # ============================================================================= # A B C # 1 1 1 0 # 2 0 0 0 # 3 -1 -1 -2 # 4 0 0 0 # =============================================================================


#map對Series中的每個元素作轉換
df["A"].map(lambda x:x+1) # ============================================================================= # 1 2.589162 # 2 0.619365 # 3 -0.273790 # 4 1.565583 # =============================================================================
相關文章
相關標籤/搜索