Python:为DataFrame中每个组的第一次观察指定值

Plu*_*ug4 1 python group-by pandas

为什么在Python中,使用Pandas,我们不能使用以下内容为每个组的第一个观察值分配值?

df['A'].groupby(df.ID).first()==0

or

df['A'].groupby(df.ID).first()==np.nan
Run Code Online (Sandbox Code Playgroud)

DataFrame的位置如下:

ID  A  
1   2
1   1
1   .45
2   .14
2   3
2   4
Run Code Online (Sandbox Code Playgroud)

换句话说,我想要

ID  A  
1   0 or NaN
1   1
1   .45
2   0 or NaN
2   3
2   4
Run Code Online (Sandbox Code Playgroud)

cumcount虽然我安装了Pandas 0.13,但对我不起作用.像其他人一样,Pandas 0.13安装有错误,我想避免处理Pandas 0.13.

Jef*_*eff 6

In [24]: df = read_csv(StringIO(data),sep='\s+')

In [25]: df
Out[25]: 
   ID     A
0   1  2.00
1   1  1.00
2   1  0.45
3   2  0.14
4   2  3.00
5   2  4.00

[6 rows x 2 columns]

In [26]: df.loc[df.groupby('ID',as_index=False).head(1).index,'A'] = np.nan

In [27]: df
Out[27]: 
   ID     A
0   1   NaN
1   1  1.00
2   1  0.45
3   2   NaN
4   2  3.00
5   2  4.00

[6 rows x 2 columns]
Run Code Online (Sandbox Code Playgroud)