我在r中有两个数据帧
df1
code date time prod price
123 01-01-2018 06:11:00 MS 12
123 01-01-2018 06:16:12 HS 13
123 01-01-2018 06:17:12 HS 13
123 01-01-2018 06:19:00 MS 12
123 02-01-2018 06:17:12 HS 13
123 02-01-2018 06:19:00 MS 12
df2
code date prod price
123 01-01-2018 MS 12
123 01-01-2018 HS 13
123 02-01-2018 HS 13
我想从df1和df2中筛选出在df1中具有唯一日期的行。我的df1
有1000多行,df2
只有4行
例如。
在df1
中有2个唯一的dates
,有两个唯一的产物MS
和HS
。所以我想比较,例如,我想过滤date和prod01-01-2018 & MS
行,然后用df2
比较相同的日期和时间
我目前正在使用for循环
unique_dates = iss_trans_268559['transaction_date'].unique()
unique_dates.sort()
unique_products = iss_trans_268559['prodcode'].unique()
for i in range(len(unique_dates)):
current_date = df1[(df1['date'] == unique_dates[i]) & df1['prod'] == unique_products[i]
df2_current = df2[df2['date'] == unique_dates[i]]
我应该在for循环中获得下面的数据帧,然后我可以比较current_date
和df2_current
中的日期
current_date
code date time prod price
123 01-01-2018 06:11:00 MS 12
123 01-01-2018 06:19:00 MS 12
df2_current
code date prod price
123 01-01-2018 MS 12
上面循环中的问题是i
将超出unique_products
和df2
的界限,我该怎么做?你知道吗
我想你需要:
您可以使用
current_date[0]
和df2_current[0]
等来访问数据帧。。。你知道吗相关问题 更多 >
编程相关推荐