[英]how to iterate through pandas row in for loop
我在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中的唯一日期过滤df1和df2中的行。 我的df1
有1000多行,而df2
只有4行
例如,在df1
有2个唯一的dates
,其中有两个唯一的产品MS
和HS
。 所以我想比较例如我想过滤日期和产品01-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
超出限制,我该怎么办?
我认为您需要:
dates= df1['date'].unique()
prods = df1['prod'].unique()
import itertools
comb = list(itertools.product(dates, prods))
# [('01-01-2018', 'MS'), ('01-01-2018', 'HS'), ('02-01-2018', 'MS'), ('02-01-2018', 'HS')]
current_date = []
df2_current = []
for i,j in comb:
current_date.append(df1[(df1['date']==i) & (df1['prod']==j)])
df2_current.append(df2[(df2['date']==i) & (df2['prod']==j)])
您可以使用current_date[0]
和df2_current[0]
访问数据df2_current[0]
依此类推...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.