熊猫:如何计算日期之前所有id的出现

时间:2019-02-28 22:48:14

标签: python pandas

我有一个具有唯一的transactionid,userid和date的数据框,我想对当前日期之前出现的所有id求和。像是df波纹管。我试图用for循环来做,但是速度很慢。有人可以帮我吗,请更有效地做。谢谢:)

df

1 个答案:

答案 0 :(得分:0)

df.loc[df['datetime] > 'your value'].count()

下面是示例

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})
#print(df)
#      A      B  C   D
# 0  foo    one  0   0
# 1  bar    one  1   2
# 2  foo    two  2   4
# 3  bar  three  3   6
# 4  foo    two  4   8
# 5  bar    two  5  10
# 6  foo    one  6  12
# 7  foo  three  7  14

newdf = df[df['C'] > 4]
print(newdf['A'].count())

# 3