日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pandas:apply(),applymap(),map()

發(fā)布時間:2023/12/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pandas:apply(),applymap(),map() 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

自己總結(jié)一下:

1.apply()

Series.apply:For applying more complex functions on a Series。

對Series的值調(diào)用函數(shù)??梢允莡func(一個適用于整個系列的NumPy函數(shù))還是一個只對單個值有效的Python函數(shù)。

>>> series = pd.Series([20, 21, 12], index=['London', ... 'New York','Helsinki']) >>> series London 20 New York 21 Helsinki 12 dtype: int64>>> def square(x): ... return x**2 >>> series.apply(square) London 400 New York 441 Helsinki 144 dtype: int64>>> series.apply(lambda x: x**2) London 400 New York 441 Helsinki 144 dtype: int64

DataFrame.apply:Apply a function row-/column-wise,按行/列方式應(yīng)用函數(shù)

沿著DataFrame的輸入軸應(yīng)用函數(shù)

>>> df.apply(numpy.sqrt) # returns DataFrame >>> df.apply(numpy.sum, axis=0) # equiv to df.sum(0) #作用的列上,可以省略 >>> df.apply(numpy.sum, axis=1) # equiv to df.sum(1) #作用在行上

2.applymap()

DataFrame.applymap:Apply a function elementwise on a whole DataFrame

在整個DataFrame上應(yīng)用函數(shù)

>>> df = pd.DataFrame(np.random.randn(3, 3)) >>> df0 1 2 0 -0.029638 1.081563 1.280300 1 0.647747 0.831136 -1.549481 2 0.513416 -0.884417 0.195343 >>> df = df.applymap(lambda x: '%.2f' % x) >>> df0 1 2 0 -0.03 1.08 1.28 1 0.65 0.83 -1.55 2 0.51 -0.88 0.20

3.map():

Series.map(arg,?na_action=None):Map values of Series using input correspondence (which can be a dict, Series, or function)

map()只要是作用將函數(shù)作用于一個Series的每一個元素,用法如下所示?

擴充:

DataFrame.aggregate(agg):only perform aggregating type operations,只執(zhí)行聚合類型操作

DataFrame.transform:only perform transformating type operations,只執(zhí)行轉(zhuǎn)換類型操作

加載數(shù)據(jù)

  • 可以看到數(shù)據(jù)包含了不同的訂單(order),以及訂單里的不同商品的數(shù)量(quantity)、單價(unit price)和總價(ext price)
  • 現(xiàn)在我們的任務(wù)是為數(shù)據(jù)表添加一列,表示不同商品在所在訂單的價錢占比。
  • 首先我們要獲得每個訂單的總花費。groupby可以實現(xiàn)。
df.groupby('order')["ext price"].sum()order 10001 576.12 10005 8185.49 10006 3724.49 Name: ext price, dtype: float64

?

?

  • 這些新得到的數(shù)據(jù)如何與原始數(shù)據(jù)幀結(jié)合呢? order_total = df.groupby('order')["ext price"].sum().rename("Order_Total").reset_index()df_1 = df.merge(order_total) df_1["Percent_of_Order"] = df_1["ext price"] / df_1["Order_Total"]

?

  • 我們實現(xiàn)了目標(還多加了一列訂單總額),但是步驟比較多,有沒有更好的辦法呢?——主角出場:)

?tramsform:

df.groupby('order')["ext price"].transform('sum')0 576.12 1 576.12 2 576.12 3 8185.49 4 8185.49 5 8185.49 6 8185.49 7 8185.49 8 3724.49 9 3724.49 10 3724.49 11 3724.49 dtype: float64

?

  • 不再是只顯示3個訂單的對應(yīng)項,而是保持了與原始數(shù)據(jù)集相同數(shù)量的項目,這樣就很好繼續(xù)了。這就是transform的獨特之處。
df["Order_Total"] = df.groupby('order')["ext price"].transform('sum') df["Percent_of_Order"] = df["ext price"] / df["Order_Total"]

甚至可以一步:?

df["Percent_of_Order"] = df["ext price"] / df.groupby('order')["extprice"].transform('sum')

?

參考:https://www.jianshu.com/p/509d7b97088c

總結(jié)

以上是生活随笔為你收集整理的pandas:apply(),applymap(),map()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。