如何浏览df1列以查看df2中满足条件的频率?

时间:2019-09-11 17:02:21

标签: python pandas dataframe count

我有两个数据帧,df1df2

df1 = pd.DataFrame({'Date': ['1/1/2017', '4/1/2017', '7/1/2017', '10/1/2017', '1/1/2018']})
df2 = pd.DataFrame({'Open Date': ['2/1/2017', '6/12/2017', '8/23/2017', '11/14/2017', '11/15/2017'], 
                    'Close Date': ['12/2/2017', '9/6/2017', '10/23/2017', '12/14/2017', '1/15/2018']})

我的目标是在df1中创建一个新列,该列指定在df1中列出的确切日期开设了多少个帐户。因此,理论上输出将如下所示:

Date      | Count
1/1/2017  |  0 Accounts open
4/1/2017  |  1
7/1/2017  |  2
10/1/2017 |  2
1/1/2018  |  1

这意味着单个帐户可以被计数一次,因为它们可以在多个确切的日期处于活动状态/打开状态。

1 个答案:

答案 0 :(得分:1)

df1['Date'] = pd.to_datetime(df1['Date'])
df2['Open Date'] = pd.to_datetime(df2['Open Date'])
df2['Close Date'] = pd.to_datetime(df2['Close Date'])

>>> df1.assign(
    Accounts_open=df1['Date'].apply(
        lambda ts: (df2['Open Date'].le(ts) & df2['Close Date'].ge(ts)).sum()))
        Date  Accounts_open
0 2017-01-01              0
1 2017-04-01              1
2 2017-07-01              2
3 2017-10-01              2
4 2018-01-01              1