简体   繁体   English

在数据框之间搜索和查找

[英]search and find between data frames

I have this data frame looking something like this: (not necessary these dates, length or order) 我的数据框看起来像这样:(不必要这些日期,长度或顺序)

     date1       date2 dummy
2015-10-01  2015-09-02     1
2015-10-01  2015-09-02     1
2015-10-03  2015-09-02     0
2015-10-04  2015-09-05     0
..........  ..........     .
..........  ..........     .
..........  ..........     .
2015-10-20  2015-11-04     1
2015-10-20  2015-11-05     1

I'm creating a new data frame containing the earliest date in 'date2' and the latest date in 'date1' and fill the period between with dates. 我正在创建一个新的数据框,其中包含“ date2”中最早的日期和“ date1”中的最新日期,并用日期填充之间的时间间隔。

startdate = df['date2'].min(axis=0)
enddate = df['date1'].max(axis=0)

def perdelta(start, end, delta):
  curr = start
  while curr <= end:
    yield curr
    curr += delta

data2 =[]
for result in perdelta(startdate, enddate, timedelta(days=1)):
   data2.append(result)

I would like to find every row of dates in the new data frame, match it up with 'date1' and count how many of the same dates that has a zero in 'dummy'. 我想找到新数据框中的每一行日期,将其与“ date1”匹配,并计算“虚拟”中有多少个相同的日期为零。 I can find all the zeros and count them for specific dates with the pandas groupby 我可以找到所有零,并使用pandas groupby计算它们的特定日期

g = df.groupby(['date1'])
df3 = pd.DataFrame(g.apply(lambda x: x[x['dummy'] == 0]['dummy'].count()), columns=['all_zeros'])

But this will only find the dates in 'date1' and count the zeros, and not start from my startdate, also it will skip dates that has a one and not paste a zero (counting non zeros should paste 0). 但这只会在'date1'中找到日期并计数零,而不是从我的起始日期开始,它也会跳过具有1的日期,而不粘贴零(计数非零应粘贴0)。

the output I would like to get is: 我想得到的输出是:

 date_newdf  count
'startdate'      0 (cuz it does not exist in date1)
 2015-09-05      0 (cuz it does not exist in date1)
 ..........      .
 ..........      .
 ..........      .
 2015-10-01      3 (found 3 zeroz with the this date)
 ..........      .
  'enddate'      2

etc. 等等

to replicate: 复制:

data = {'date1': ['15-10-01', '15-10-01', '15-10-03', '15-10-04', '15-10-05', '15-10-05'],
    'date2': ['15-09-02', '15-09-02', '15-09-02', '15-09-05', '15-09-05', '15-09-05'],
    'dummy': [1,1,0,0,0,1]}
df = pd.DataFrame(data, columns=['date1', 'date2' , 'dummy'])    

I think, your need add reindex function with list data2 to the end of your script and then fill missing data NaN to 1 . 我认为,您需要在脚本末尾添加带有列表data2 reindex函数,然后将缺失的数据NaN填充为1

Input for better testing: 输入更好的测试:

       date1      date2  dummy
0 2015-10-01 2015-09-02      1
1 2015-10-01 2015-09-02      1
2 2015-10-03 2015-09-02      0
3 2015-10-04 2015-09-05      0
4 2015-10-05 2015-11-05      0
5 2015-10-05 2015-11-05      0
6 2015-10-05 2015-11-05      0
7 2015-10-05 2015-11-05      1
8 2015-10-05 2015-11-05      1
print df3
            all_zeros
date1                
2015-10-01          0
2015-10-03          1
2015-10-04          1
2015-10-05          3

df3 = df3.reindex(pd.DatetimeIndex(data2))
df3 = df3.fillna(0)
print df3
            all_zeros
2015-09-02          0
2015-09-03          0
2015-09-04          0
2015-09-05          0
2015-09-06          0
2015-09-07          0
2015-09-08          0
2015-09-09          0
2015-09-10          0
2015-09-11          0
2015-09-12          0
2015-09-13          0
2015-09-14          0
2015-09-15          0
2015-09-16          0
2015-09-17          0
2015-09-18          0
2015-09-19          0
2015-09-20          0
2015-09-21          0
2015-09-22          0
2015-09-23          0
2015-09-24          0
2015-09-25          0
2015-09-26          0
2015-09-27          0
2015-09-28          0
2015-09-29          0
2015-09-30          0
2015-10-01          0
2015-10-02          0
2015-10-03          1
2015-10-04          1
2015-10-05          3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM