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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python transform_Pandas的数据分组-transform函数

發(fā)布時(shí)間:2023/12/19 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python transform_Pandas的数据分组-transform函数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

31. Pandas的數(shù)據(jù)分組-transform函數(shù)

pandas有些函數(shù)在不同的版本下可能使用功能有些不同,這是個(gè)麻煩事情。查看pandas的版本。

$ python

>>> import pandas as pd

>>> print pd.__version__

0.17.1

升級(jí)pandas

$ sudo pip install -U pandas

或者安裝指定版本的軟件:

$ sudo pip install pandas=x.y.z

x.y.z為選用的pandas的版本號(hào)。而本章的transform函數(shù)是在pandas的0.20版本后才加入pandas的。

transform函數(shù)可以作用于groupby之后的每個(gè)組的所有數(shù)據(jù)。

import pandas as pd

import numpy as np

idx = [101,101,101,102,102,102,103,103,103]

idx += [101,102,103] * 3

name = ["apple","pearl","orange", "apple","pearl","orange","apple","pearl","orange"]

name += ["apple"] * 3 + ["pearl"] * 3 + ["orange"] * 3

price = [4.1,5.3,6.3,4.20,5.4,6.0,4.5,5.5,6.8]

price += [4] * 3 + [5] * 3 + [6] * 3

df0 = pd.DataFrame({ "fruit": name, "price" : price, "supplier" :idx})

print "df", "*" * 30

print df0

def p_data(o):

for name, group in o:

print name

print group[:3]

dg1 = df0.groupby(["fruit"])

print "1", "*" * 30

print p_data(dg1)

def f1(x):

return x + 1

def f2(x):

return x + 100

print "2", "*" * 30

print dg1["price"].transform(f1)[:3]

print "3", "*" * 30

print dg1["supplier"].transform(f2)[:3]

print "4", "*" * 30

print dg1.transform(f2)[:3]

示例里的print dg1.transform(f2)[:3]語(yǔ)句是對(duì)dg1各組里各個(gè)值都在原有基礎(chǔ)上加100,從執(zhí)行結(jié)果可以看出是這樣的。

df ******************************

fruit price supplier

0 apple 4.1 101

1 pearl 5.3 101

2 orange 6.3 101

3 apple 4.2 102

4 pearl 5.4 102

5 orange 6.0 102

6 apple 4.5 103

7 pearl 5.5 103

8 orange 6.8 103

9 apple 4.0 101

10 apple 4.0 102

11 apple 4.0 103

12 pearl 5.0 101

13 pearl 5.0 102

14 pearl 5.0 103

15 orange 6.0 101

16 orange 6.0 102

17 orange 6.0 103

1 ******************************

apple

fruit price supplier

0 apple 4.1 101

3 apple 4.2 102

6 apple 4.5 103

orange

fruit price supplier

2 orange 6.3 101

5 orange 6.0 102

8 orange 6.8 103

pearl

fruit price supplier

1 pearl 5.3 101

4 pearl 5.4 102

7 pearl 5.5 103

2 ******************************

0 5.1

1 6.3

2 7.3

Name: price, dtype: float64

3 ******************************

0 201

1 201

2 201

Name: supplier, dtype: int64

4 ******************************

price supplier

0 104.1 201

1 105.3 201

2 106.3 201

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的python transform_Pandas的数据分组-transform函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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