我有一个数据框:
df = pd.DataFrame({'start': [50, 100, 50000, 50030, 100000],
'end': [51, 101, 50001, 50031, 100001],
'value': [1, 2, 3, 4, 5]},
index=['id1', 'id2', 'id3', 'id4', 'id5'])
x y z
id1 foo bar 1
id2 bar me 2
id3 you bar 3
id4 foo you 4
id5 bar foo 5
以及排列列表:
l = [(foo, bar), (bar, foo)]
我想提取包含列 [x,y] 中排列的所有行:
(foo, bar) -> id1, foo, bar, 1
(bar, foo) -> id5, bar, foo, 5
如何根据两个值提取这些行?
您可以将x,y
列转换为,因此可以通过withMultiIndex
比较值:Index.isin
boolean indexing
l = [('foo', 'bar'), ('bar', 'foo')]
df1 = df[df.set_index(['x','y']).index.isin(l)]
print (df1)
x y z
id1 foo bar 1
id5 bar foo 5
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句