Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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 使用groupby创建新列以指示特定列上的趋势_Pandas_Pandas Groupby - Fatal编程技术网

Pandas 使用groupby创建新列以指示特定列上的趋势

Pandas 使用groupby创建新列以指示特定列上的趋势,pandas,pandas-groupby,Pandas,Pandas Groupby,我有一个如下所示的数据框 Session ID cumulative_prob s1 1 0.4 s1 3 0.9 s1 4 -0.1 s1 5 0.3 s1 8 1.2 s1 9 0.2 s2 22 0.4 s

我有一个如下所示的数据框

Session        ID       cumulative_prob
s1              1       0.4
s1              3       0.9
s1              4      -0.1
s1              5       0.3
s1              8       1.2
s1              9       0.2
s2              22      0.4
s2              29      0.7
s2              31      1.4
s2              32      0.4
s2              34      0.9
s3              36      0.9
s3              37     -0.1
s3              38      0.2
s3              40      1.0
从中,我想创建一个新的列,指示会话趋势(增加或减少)

预期产出:

Session        ID       cumulative_prob     Decrease
    s1              1       0.4             no
    s1              3       0.9             no
    s1              4      -0.1             yes
    s1              5       0.3             no
    s1              8       1.2             no
    s1              9       0.2             yes
    s2              22      0.4             no
    s2              29      0.7             no
    s2              31      1.4             no
    s2              32      0.4             yes
    s2              34      0.9             no
    s3              36      0.9             no
    s3              37     -0.1             yes
    s3              38      0.2             no
    s3              40      1.0             no
注意:为每个会话的第一行保留“否”,并且:

我们还可以使用:


如果可能,请看下面的问题。。没有人回答。您实际上不需要
fillna(0).lt(0)
nan
的计算结果仍然为
False
#import numpy as np
df['Decrease'] = np.where(df.groupby('Session')['cumulative_prob']
                            .diff()
                            .lt(0),
                          'yes',
                          'no')

print(df)
   Session  ID  cumulative_prob Decrease
0       s1   1              0.4       no
1       s1   3              0.9       no
2       s1   4             -0.1      yes
3       s1   5              0.3       no
4       s1   8              1.2       no
5       s1   9              0.2      yes
6       s2  22              0.4       no
7       s2  29              0.7       no
8       s2  31              1.4       no
9       s2  32              0.4      yes
10      s2  34              0.9       no
11      s3  36              0.9       no
12      s3  37             -0.1      yes
13      s3  38              0.2       no
14      s3  40              1.0       no
(df.groupby('Session')['cumulative_prob']
   .diff()
   .lt(0)
   .map({True : 'yes' , False : 'no'}))