Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas 数据分析_Pandas_Dataframe_Statistics - Fatal编程技术网

Pandas 数据分析

Pandas 数据分析,pandas,dataframe,statistics,Pandas,Dataframe,Statistics,我正在对一个非常广泛的数据集进行分析,该数据集具有属性(g、month、p),这些属性是使用pandas的groupby按组组织的 G month p G1 1 0.040698496 G1 2 0.225640771 G1 3 0.236948047 G1 4 0.119339576 G1 5 0.779272432 G2 1 0.892168636 G2 2 0.062467967 G2 3 0.936044226 G3 1 0.

我正在对一个非常广泛的数据集进行分析,该数据集具有属性(g、month、p),这些属性是使用pandas的groupby按组组织的

G   month   p
G1  1   0.040698496
G1  2   0.225640771
G1  3   0.236948047
G1  4   0.119339576
G1  5   0.779272432
G2  1   0.892168636
G2  2   0.062467967
G2  3   0.936044226
G3  1   0.509212613
G3  2   0.476718744
G3  3   0.407299543
G3  4   0.843260893
G4  1   0.882554249
然后,我按G组从1到n提取统计数据,如下所示

    g1  g2  g3  gn
mean    0.280379864 0.630226943 0.559122948 …
std 0.290326376 0.49218285  0.194135874 …
count   5   3   4   …
需要创建一个新字段,该字段是组平均值与变量p的乘积,有一些方法可以使其自动…由于扩展(超过200个组),单独执行会花费大量时间。 预期产量为

G   month   p   STD*p
G1  1   0.040698496 0.011815847
G1  2   0.225640771 0.065509467
G1  3   0.236948047 0.068792268
G1  4   0.119339576 0.034647427
G1  5   0.779272432 0.226243341
G2  1   0.892168636 0.439110102
G2  2   0.062467967 0.030745662
G2  3   0.936044226 0.460704915
G3  1   0.509212613 0.098856436
G3  2   0.476718744 0.09254821
G3  3   0.407299543 0.079071453
G3  4   0.843260893 0.16370719
std
一起使用可重复聚合值,因此可以通过
p
列进行倍数:

df['STD*p'] = df.groupby('G')['p'].transform('std').mul(df['p'])
print (df)
     G  month         p     STD*p
0   G1      1  0.040698  0.011816
1   G1      2  0.225641  0.065509
2   G1      3  0.236948  0.068792
3   G1      4  0.119340  0.034647
4   G1      5  0.779272  0.226243
5   G2      1  0.892169  0.439110
6   G2      2  0.062468  0.030746
7   G2      3  0.936044  0.460705
8   G3      1  0.509213  0.098856
9   G3      2  0.476719  0.092548
10  G3      3  0.407300  0.079071
11  G3      4  0.843261  0.163707
12  G4      1  0.882554       NaN

例如,如果我想用一个等式乘以列,比如(std*p)/len('p')?@KarelSanchezHernandez-不,它只会重复
std
value,如果需要这个公式add
.div(df.groupby('G')['p'])。transform('size'))
-如果需要,每个groups@KarelSanchezHernandez-您可以检查它是如何工作的
df.groupby('G')['p'].transform('std')
vs
df.groupby('G')['p'].std()