Select rows from a DataFrame based on multiple values in a column in pandas
本问题已经有最佳答案,请猛点这里访问。
这不是重复的问题,但类似于
Select rows from a DataFrame based on values in a column in pandas
在上一个链接的回答中,它只基于一个标准,如果我有多个标准该怎么办。
我想在一个列中选择多行,而不仅仅是基于特定值的一行。为了争论起见,考虑一下世界银行的数据框架
1 2 3 4 | import pandas.io.wb as wb import pandas as pd import numpy as np df2= wb.get_indicators() |
我选择certian值的方式是这样的
1 | df2.loc[df2['id'] == 'SP.POP.TOTL'] |
和
1 | df2.loc[df2['id'] == 'NY.GNP.PCAP.CD'] |
如何在一个新的数据帧中同时选择,或者说3或4?这样,行是:
1 2 | 'SP.POP.TOTL' 'NY.GNP.PCAP.CD' |
提前谢谢你
您可以使用.isin():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | In [28]: df2[df2['id'].isin(['SP.POP.TOTL','NY.GNP.PCAP.CD'])] Out[28]: id name \ 7478 NY.GNP.PCAP.CD GNI per capita, Atlas method (current US$) 9568 SP.POP.TOTL Population, total source \ 7478 World Development Indicators 9568 World Development Indicators sourceNote \ 7478 GNI per capita (formerly GNP per capita) is th... 9568 Total population is based on the de facto defi... sourceOrganization \ 7478 b'World Bank national accounts data, and OECD ... 9568 b'(1) United Nations Population Division. Worl... topics 7478 Economy & Growth ; Climate Change 9568 Health ; Climate Change |