我有一个数据框,想在两列上使用.isin()来过滤掉某些行。
df =
amount currency1 currency2
50 GBP nan
47 nan USD
30 nan GBP
60 EUR nan
57 nan EUR
如果currency1中填充了一个值,则currency2将不是,反之亦然。我想使用isin()仅选择GBP和USD。我尝试使用:
df = df.loc[df['currency1'].isin(['GBP','USD'])]
,但是然后将具有GBP2或Currency2货币的行从数据框中删除,因此我想使用“或”语句在两列中查找GBP和USD,以使数据框看起来像:
amount currency1 currency2
50 GBP nan
47 nan USD
30 nan GBP
答案 0 :(得分:1)
df[df.apply(lambda x:x.isin(['GBP','USD'])).any(1)]
amount currency1 currency2
0 50 GBP NaN
1 47 NaN USD
2 30 NaN GBP
答案 1 :(得分:0)
您正在寻找any
,它可以将一行的行或列的布尔值减少为单个布尔值。在这种情况下,请axis=1
逐行使用。
cols = ['currency1', 'currency2'] # columns to search
currencies = ['GBP', 'USD'] # currencies to search for
df[df[cols].isin(currencies).any(axis=1)]
结果:
amount currency1 currency2
0 50 GBP NaN
1 47 NaN USD
2 30 NaN GBP