[英]python pandas : for each element in a list return a row from dataframe based on conditions
I have two dataframes (df_key,df2_values) and would like to get rows from df_source for each row in df_key. 我有两个数据框(df_key,df2_values),并且想从df_source中为df_key中的每一行获取行。
I can do a lookup for one key at a time but that is inefficient considering that the I have to loop through rows in df_key and then call a function to appropriate rows back from df_source. 我可以一次查找一个键,但是考虑到我必须循环遍历df_key中的行,然后从df_source调用一个函数以返回适当的行,因此效率低下。
df_source[((df_source["cond1"] == key) | (df_source["cond2"] == key)) & (df_source["cond3"] == "values") ]
Any suggestions??? 有什么建议么???
I think you need isin
by column in df_key
: 我认为你需要
isin
通过列df_key
:
df = df_source[((df_source["cond1"].isin(df_key['key'])) |
(df_source["cond2"].isin(df_key['key']))) & (df_source["cond3"] == "values")]
Sample: 样品:
df_source = pd.DataFrame({'A':list('abcdef'),
'cond1':[4,5,4,5,5,4],
'cond2':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0],
'E':[5,3,6,9,2,4],
'cond3':list('aaabbb')})
df_key = pd.DataFrame({
'A': ['a','a','a','a','b','b','b','c','d'],
'key': [5,3,6,9,2,4,1,2,3]
})
df = df_source[((df_source["cond1"].isin(df_key['key'])) |
(df_source["cond2"].isin(df_key['key']))) & (df_source["cond3"] == "a") ]
print (df)
A D E cond1 cond2 cond3
0 a 1 5 4 7 a
1 b 3 3 5 8 a
2 c 5 6 4 9 a
EDIT: 编辑:
You need filter all unique values for keys
use numpy.intersect1d
, but is necessary also filter by condition: 您需要使用
numpy.intersect1d
过滤keys
所有唯一值,但还必须按条件过滤:
vals = df_source.loc[df_source["cond3"] == "a", ['cond1','cond2']].values.ravel()
print (vals)
[4 7 5 8 4 9]
L = np.intersect1d(vals, df_key['key'].values).tolist()
print (L)
[4, 5, 9]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.