简体   繁体   中英

how can i get compare two date columns with the same category in pandas

I'm very new to pandas sorry for not making much sense. I have a sense that groupby by the category but, I am not sure how to run functions within groupby.

I want to find the dates from a given row in Date1 and see if any dates(in date2) of the same id is within 7 days.

I thought about splitting date1 and date2 by doing but i'm not sure where to go from there.

g1 = df[['Category', 'Date1']]

g2 = df[['Category', 'Date2']]

dif = pd.Timedelta(7, unit='D')
df['isDateWithin7Days'] = np.where((g1['Category'] == g2['Category'])(df['Date1'] > g2['Date2']-dif, True, False))

I get this error

ValueError: operands could not be broadcast together with shapes (50537,) (3,)

df1:

category        date1        date2      
  blue          1/1/2018     
  blue                       1/2/2018
  blue                       1/5/2018
  blue          2/1/2018
  green         1/3/2018     
  green                      1/1/2018
  red           12/1/2018
  red                        11/1/2018

Expected results:

category        date1        date2     isDateWithin7Days?      EarliestDate?
  blue          1/1/2018                      True             1/2/2018
  blue          2/1/2018                      False               0
  green         1/3/2018                      False               0
  red           12/1/2018                     False               0

IIUC, you are trying to look for dates in the date2 column that are within 7 days of a unique combination of category and date1 - this code returns True if any such dates are found, else returns False :

df['date1'] = pd.to_datetime(df['date1'], format = '%m-%d-%y')
df['date2'] = pd.to_datetime(df['date2'], format = '%m-%d-%y')

df1 = df.dropna(subset = ['date1']).drop(columns = ['date2'])
df2 = df.dropna(subset = ['date2']).drop(columns = ['date1'])

df3 = df1.merge(df2, on = 'category')
df3['date2'].between(df3['date1'] - pd.Timedelta(days=7), df3['date1'] + pd.Timedelta(days=7))

df3['isDateWithin7Days?'] = df3['date2'].between(df3['date1'] - pd.Timedelta(days=7), df3['date1'] + pd.Timedelta(days=7))
df3 = df3.groupby(['category', 'date1'])['isDateWithin7Days?'].sum().reset_index()
df3['isDateWithin7Days?'] = np.where(df3['isDateWithin7Days?'] > 0, True, False)

Output:

  category      date1  isDateWithin7Days?
0     blue 2018-01-01                True
1     blue 2018-02-01               False
2    green 2018-01-03               False
3      red 2018-12-01               False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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