比较两个独立数据帧的时间

2024-10-06 11:20:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个独立的时间数据帧。我想把广告日期付给每个人的钱加起来。以下是示例数据集:

Name        Ad Date     Ad Number      
---------------------------------
Michael    4/08/2018        1    
Tony       4/08/2018        1
Alex       4/08/2018        1
Alex       6/08/2018        2
Vanessa    9/08/2018        1

Name        Date         Payments  
--------------------------------------
Michael    4/08/2018      100
Tony       4/08/2018      200
Alex       4/06/2018      300
Alex       6/06/2018      400
Alex       6/07/2018      400
Vanessa    9/08/2018      500

以下是所需的输出:


Name        Ad Number     Payments    
------------------------------------
Michael        1           100
Tony           1           200 
Alex           1           300
Alex           2           800
Vanessa        1           500

所以,如果你看看亚历克斯,一共有3笔钱是为2个广告支付的,我想用广告的日期范围来计算这些钱的总和


Tags: 数据name示例numberdate时间ad广告
1条回答
网友
1楼 · 发布于 2024-10-06 11:20:51

虽然这可能不是最强大的解决方案,但我想到了

#Merge two lists
df_new = df.merge(df1,how='outer',on='Name')

#logic to make sure the sum is between the current date and after the previous date
count = 0
temp_list = []
while count != len(df_new): 
  names = df_new.Name[count]
  find = df_new[['Name','Ad Number','Ad Date_x','Ad Date_y','Payments']] [(df_new['Name'] == names)]
  find['new_col'] = np.where(find['Ad Date_y']<=find['Ad Date_x'], 'yes', 'no')
  x = find.sort_values('Ad Date_y')
  x = x[x.new_col == 'yes']
  x = x.drop_duplicates('Ad Date_y')
  x = x.groupby(['Name','Ad Number','Ad Date_x'],as_index=False)['Payments'].sum()
  intermediate_list = x.values.tolist()
  temp_list.append(intermediate_list)
  count += 1

#Traversing through list of lists and appending to final list to make another df
final_list = []
for i in temp_list:
  for j in i:
    final_list.append(j)

#create final df and drop duplicates and drop the Ad_Date Columns
final_df = pd.DataFrame(final_list, columns = ['Name','Ad_Number','Ad_Date','Payments'])
final_df = final_df.drop_duplicates(['Name','Ad_Number','Ad_Date','Payments'])
final_df = final_df.drop('Ad_Date',axis=1)

print(final_df)

#RESULT

Name      Ad_Number  Payments
Michael           1       100
Tony              1       200
Alex              1       300
Alex              2       800
Vanessa           1       500

相关问题 更多 >