Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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
Python 熊猫:列举每组中的项目_Python_Pandas_Pandas Groupby - Fatal编程技术网

Python 熊猫:列举每组中的项目

Python 熊猫:列举每组中的项目,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我有一个像 id chi prop ord 0 100 L 67 0 1 100 L 68 1 2 100 L 68 2 3 100 L 68 3 4 100 L 70 0 5 100 L 71 0 6 100 R 67 0 7 100 R 68 1 8 100 R 68

我有一个像

    id   chi  prop   ord 
0   100   L    67     0 
1   100   L    68     1 
2   100   L    68     2 
3   100   L    68     3 
4   100   L    70     0 
5   100   L    71     0 
6   100   R    67     0 
7   100   R    68     1 
8   100   R    68     2 
9   100   R    68     3 
10  110   R    70     0 
11  110   R    71     0 
12  101   L    67     0 
13  101   L    68     0 
14  101   L    69     0 
15  101   L    71     0 
16  101   L    72     0 
17  201   R    67     0 
18  201   R    68     0 
19  201   R    69     0
当(
prop
chi
id
)都具有相同的值时,
ord
基本上给出了条目的顺序。但这不是我想要的。相反,我希望能够枚举
{(id,chi)}
中从0到n的每个组g的条目,其中n是组g的大小。所以我想得到一个

    id   chi  prop   count 
0   100   L    67     0 
1   100   L    68     1 
2   100   L    68     2 
3   100   L    68     3 
4   100   L    70     4 
5   100   L    71     5 
6   100   R    67     0 
7   100   R    68     1 
8   100   R    68     2 
9   100   R    68     3 
10  110   R    70     0 
11  110   R    71     1 
12  101   L    67     0 
13  101   L    68     1 
14  101   L    69     2 
15  101   L    71     3 
16  101   L    72     4 
17  201   R    67     0 
18  201   R    68     1 
19  201   R    69     2
我想知道是否有一种简单的方法可以使用
pandas
实现这一点。下面的内容非常接近,但感觉太复杂了,出于某种原因,它不允许我将结果数据帧与原始数据帧连接起来

(df.groupby(['id', 'chi'])
   .apply(lambda g: np.arange(g.shape[0]))
   .apply(pd.Series, 1)
   .stack()
   .rename('counter')
   .reset_index()         
   .drop(columns=['level_2']))
编辑:第二种方式当然是
for
循环方式,但我正在寻找比以下方式更“Pythonic”的方式:

R使用
tidyverse
软件包有一个非常简单的方法来实现这种行为,但是我还没有找到一个很好的方法来实现
pandas
的同样效果。非常感谢您提供的任何帮助

cumcount

defaultdict
count
cumcount

defaultdict
count

查看groupby cumcount
df['count']=df.sort_值(['prop','ord']).groupby(['id','chi']).cumcount()
查看groupby cumcount
df['count']=df.sort_值(['prop','ord']).groupby(['id','chi']).cumcount()
for gname, idx in df.groupby(['id','chi']).groups.items():
    tmp = df.loc[idx]
    df.loc[idx, 'counter'] = np.arange(tmp.shape[0])
df.assign(ord=df.groupby(['id', 'chi']).cumcount())

     id chi  prop  ord
0   100   L    67    0
1   100   L    68    1
2   100   L    68    2
3   100   L    68    3
4   100   L    70    4
5   100   L    71    5
6   100   R    67    0
7   100   R    68    1
8   100   R    68    2
9   100   R    68    3
10  110   R    70    0
11  110   R    71    1
12  101   L    67    0
13  101   L    68    1
14  101   L    69    2
15  101   L    71    3
16  101   L    72    4
17  201   R    67    0
18  201   R    68    1
19  201   R    69    2
from itertools import count
from collections import defaultdict

d = defaultdict(count)

df.assign(ord=[next(d[t]) for t in zip(df.id, df.chi)])

     id chi  prop  ord
0   100   L    67    0
1   100   L    68    1
2   100   L    68    2
3   100   L    68    3
4   100   L    70    4
5   100   L    71    5
6   100   R    67    0
7   100   R    68    1
8   100   R    68    2
9   100   R    68    3
10  110   R    70    0
11  110   R    71    1
12  101   L    67    0
13  101   L    68    1
14  101   L    69    2
15  101   L    71    3
16  101   L    72    4
17  201   R    67    0
18  201   R    68    1
19  201   R    69    2