根据pandas中列中的多个值从DataFrame中选择行

rsc*_*c05 5 python dataframe pandas

这不是一个重复的问题,但类似于

根据pandas中列的值从DataFrame中选择行

在上一个链接的答案中,如果我有多个标准,它只基于一个标准.

我想在列中选择许多行,而不仅仅是基于特定值的行.为了论证,考虑来自世界银行的DataFrame

import pandas.io.wb as wb
import pandas as pd
import numpy as np
df2= wb.get_indicators()
Run Code Online (Sandbox Code Playgroud)

我选择certian值的方式就是这样

df2.loc[df2['id'] == 'SP.POP.TOTL']
Run Code Online (Sandbox Code Playgroud)

df2.loc[df2['id'] == 'NY.GNP.PCAP.CD']
Run Code Online (Sandbox Code Playgroud)

如何在一个新数据帧中选择或者说3或4?这样行是:

'SP.POP.TOTL'
'NY.GNP.PCAP.CD'
Run Code Online (Sandbox Code Playgroud)

先感谢您

Max*_*axU 7

你可以使用.isin():

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
Run Code Online (Sandbox Code Playgroud)