【发布时间】:2016-02-20 03:37:50
【问题描述】:
类似但不一样:Selecting rows - based on a list - from a DF with duplicated columns
我有两个 dfs:
df1 = pd.DataFrame({'total': [25, 45, 75, 36, 45]},
index=['base', 'c', 'd', 'base', 'e'])
total
base 25
c 45
d 75
base 36
e 45
df2 = pd.DataFrame({'type': ['rc', 'rc', 'c%', 'c%', 'pp%']},
index=['base', 'c', 'd', 'base', 'e'])
type
base rc
c rc
d c%
base c%
e pp%
我想从 df1 获取 df2 中值为 'c%' 和/或 'pp%' 的行。
这就是我的做法
keep = df2[df2['type'].isin(['c%', 'pp%'])].index
Index([u'd', u'base', u'e'], dtype='object')
df1.loc[keep]
total
d 75
base 25
base 36
e 45
'base 25' 不应该在那里,但因为我使用标签,所以我理解它为什么在那里。
期望的结果:
total
d 75
base 36
e 45
如何更改我的代码来处理这个问题?
【问题讨论】:
标签: python pandas indexing filter dataframe