[英]pandas select dataframe rows according to pair condition
I have a dataframe like following:我有一个 dataframe 如下所示:
df = pd.DataFrame({
'contract':[11, 11, 11, 12, 12, 13, 13, 13, 13],
'num_date':[1, 2, 3, 1, 2, 1, 2, 3, 4],
'val': [100, 110, 120, 50, 150, 95, 105, 115, 125]
})
Pairs (contracts, num_date) are unique对(合同,num_date)是唯一的
Also I have the list of tuples:我也有元组列表:
filt = [(11, 1),
(11, 1),
(12, 3),
(12, 2),
(13, 3),
(13, 1)]
Each tuple in list is a pair (contract, num_date) for dataframe above.列表中的每个元组都是上面 dataframe 的一对 (contract, num_date)。
I need choose rows from dataframe according to this list as a condition, and if the tuple is repeated several times then the row corresponding to it also must be repeated the same number of times.我需要根据此列表从 dataframe 中选择行作为条件,如果元组重复多次,则与其对应的行也必须重复相同的次数。
For example for dataframe and list of tuples above solution is例如对于 dataframe 和上述解决方案的元组列表是
contract合同 | num_date num_date | val值 |
---|---|---|
11 11 | 1 1 | 100 100 |
11 11 | 1 1 | 100 100 |
12 12 | 2 2 | 150 150 |
13 13 | 3 3 | 115 115 |
13 13 | 1 1 | 95 95 |
Will be better if the solution will be time-efficient如果解决方案具有时间效率会更好
First you need to convert the tuples into a dataframe:首先,您需要将元组转换为 dataframe:
df2 = pd.DataFrame(filt)
df2.columns = ['contract', 'num_date']
Then merge between the two and drop nulls:然后在两者之间合并并删除空值:
df2 = df2.merge(right=df, on = ['contract', 'num_date'], how='left')
df2.dropna(subset=['val'], inplace=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.