gpt4 book ai didi

python - Pandas 中两个特定日期时间范围之间出现的数字

转载 作者:行者123 更新时间:2023-12-01 00:13:49 28 4
gpt4 key购买 nike

我有 2 个 CSV 文件,如下所示。

  1. 我想要一个新列差异,其中...
    • 如果手机号码出现在 Book_date...App_date 的日期范围内:差异 = 差异App_date > 和 Occur_date
    • 如果未在该日期范围内发生,则返回 NaN。
  2. 我还想根据唯一类别和手机号码对其进行过滤

csv_1

Mobile_Number    Book_Date       App_Date

503477334 2018-10-12 2018-10-18
506002884 2018-10-12 2018-10-19
501022162 2018-10-12 2018-10-16
503487338 2018-10-13 2018-10-13
506012887 2018-10-13 2018-10-21
503427339 2018-10-14 2018-10-17

csv_2

Mobile_Number    Occur_Date    

503477334 2018-10-16
506002884 2018-10-21
501022162 2018-10-15
503487338 2018-10-13
501428449 2018-10-18
506012887 2018-10-14

我想要在 csv_1 中添加一个新列,其中如果手机号码出现在 csv_2 中的 Book_date 和 App_date 的日期范围内,则 App_date 与 Occur_date 之间的差值或 NaN(如果该手机号码未出现在该日期范围内)。输出应该是

输出

Mobile_Number    Book_Date       App_Date   Difference

503477334 2018-10-12 2018-10-18 2
506002884 2018-10-12 2018-10-19 -2
501022162 2018-10-12 2018-10-16 1
503487338 2018-10-13 2018-10-13 0
506012887 2018-10-13 2018-10-21 7
503427339 2018-10-14 2018-10-17 NaN

编辑:

如果我想根据上述两个 csv 文件的唯一类别和 mobile_number 对其进行过滤。如何做同样的事情?

csv_1

Category     Mobile_Number   Book_Date       App_Date

A 503477334 2018-10-12 2018-10-18
B 503477334 2018-10-07 2018-10-16
C 501022162 2018-10-12 2018-10-16
A 503487338 2018-10-13 2018-10-13
C 506012887 2018-10-13 2018-10-21
E 503427339 2018-10-14 2018-10-17

csv_2

Category     Mobile_Number    Occur_Date    

A 503477334 2018-10-16
B 503477334 2018-10-13
A 501022162 2018-10-15
A 503487338 2018-10-13
F 501428449 2018-10-18
C 506012887 2018-10-14

我希望根据 Mobile_Number 和类别过滤输出

输出

Category     Mobile_Number    Book_Date       App_Date   Difference

A 503477334 2018-10-12 2018-10-18 2
B 503477334 2018-10-07 2018-10-16 3
C 501022162 2018-10-12 2018-10-16 NaN
A 503487338 2018-10-13 2018-10-13 0
C 506012887 2018-10-13 2018-10-21 7
E 503427339 2018-10-14 2018-10-17 NaN

最佳答案

使用Series.map对于与 Mobile_Number 匹配的新 Series 以及列之间的测试值,请使用 Series.between ,然后通过掩码使用 numpy.where 赋值:

df1['Book_Date'] = pd.to_datetime(df1['Book_Date'])
df1['App_Date'] = pd.to_datetime(df1['App_Date'])
df2['Occur_Date'] = pd.to_datetime(df2['Occur_Date'])

s1 = df2.drop_duplicates('Mobile_Number').set_index('Mobile_Number')['Occur_Date']
s2 = df1['Mobile_Number'].map(s1)

m = s2.between(df1['Book_Date'], df1['App_Date'])

#solution with no mask
df1['Difference1'] = df1['App_Date'].sub(s2).dt.days
#solution with test between
df1['Difference2'] = np.where(m, df1['App_Date'].sub(s2).dt.days, np.nan)
print (df1)
Mobile_Number Book_Date App_Date Difference Difference1 Difference2
0 503477334 2018-10-12 2018-10-18 2018-10-16 2.0 2.0
1 506002884 2018-10-12 2018-10-19 2018-10-21 -2.0 NaN
2 501022162 2018-10-12 2018-10-16 2018-10-15 1.0 1.0
3 503487338 2018-10-13 2018-10-13 2018-10-13 0.0 0.0
4 506012887 2018-10-13 2018-10-21 2018-10-14 7.0 7.0
5 503427339 2018-10-14 2018-10-17 NaT NaN NaN

编辑:

您可以使用 merge 代替 map 来连接 2 列:

df1['Book_Date'] = pd.to_datetime(df1['Book_Date'])
df1['App_Date'] = pd.to_datetime(df1['App_Date'])
df2['Occur_Date'] = pd.to_datetime(df2['Occur_Date'])

df3 = df1.merge(df2, on=['Category','Mobile_Number'], how='left')
print (df3)
Category Mobile_Number Book_Date App_Date Occur_Date
0 A 503477334 2018-10-12 2018-10-18 2018-10-16
1 B 503477334 2018-10-07 2018-10-16 2018-10-13
2 C 501022162 2018-10-12 2018-10-16 NaT
3 A 503487338 2018-10-13 2018-10-13 2018-10-13
4 C 506012887 2018-10-13 2018-10-21 2018-10-14
5 E 503427339 2018-10-14 2018-10-17 NaT

m = df3['Occur_Date'].between(df3['Book_Date'], df3['App_Date'])
#print (m)

df3['Difference2'] = np.where(m, df3['App_Date'].sub(df3['Occur_Date']).dt.days, np.nan)
print (df3)
Category Mobile_Number Book_Date App_Date Occur_Date Difference2
0 A 503477334 2018-10-12 2018-10-18 2018-10-16 2.0
1 B 503477334 2018-10-07 2018-10-16 2018-10-13 3.0
2 C 501022162 2018-10-12 2018-10-16 NaT NaN
3 A 503487338 2018-10-13 2018-10-13 2018-10-13 0.0
4 C 506012887 2018-10-13 2018-10-21 2018-10-14 7.0
5 E 503427339 2018-10-14 2018-10-17 NaT NaN

关于python - Pandas 中两个特定日期时间范围之间出现的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59456738/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com