我有一个数据框:
df = pd.DataFrame({'dates':['2015-01-01','2015-01-02','2015-01-03','2015-01-03','2015-01- 02','2015-01-02','2015-01-01'],'myvals':[1,2,3,3,4,4,3]})
我想和分组依据一起算:df.groupby('dates')['myval'].transform('nunique')
但是我只想在myval=3
所需的输出:
dates myvals
2015-01-01 1
2015-01-02 0
2015-01-03 2
2015-01-03 2
2015-01-02 0
2015-01-02 0
2015-01-01 1
在这种情况下,如何修改代码。
谢谢!
答案 0 :(得分:1)
使用:
df['myvals'] = (df['myvals'] == 3).groupby(df['dates']).transform('sum')
输出:
dates myvals
0 2015-01-01 1.0
1 2015-01-02 0.0
2 2015-01-03 2.0
3 2015-01-03 2.0
4 2015-01-02 0.0
5 2015-01-02 0.0
6 2015-01-01 1.0