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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36410075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:59:38  来源:igfitidea点击:

Select rows from a DataFrame based on multiple values in a column in pandas

pythonpandasdataframe

提问by rsc05

This is not a repetitive question, yet similar to

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

Select rows from a DataFrame based on values in a column in pandas

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

In that answer up in the previous link it is only based on one criteria what if I have more than one criteria.

在上一个链接中的那个答案中,它仅基于一个标准,如果我有多个标准,那该怎么办。

I would like to select many rows in a column not only one based on particular values. For the sake of argument consider the DataFrame from the World Bank

我想在一列中选择多行,而不仅仅是基于特定值的一行。为了论证起见,请考虑世界银行的 DataFrame

import pandas.io.wb as wb
import pandas as pd
import numpy as np
df2= wb.get_indicators()

The way I select a certian value is as so

我选择certian值的方式是这样

df2.loc[df2['id'] == 'SP.POP.TOTL']

and

df2.loc[df2['id'] == 'NY.GNP.PCAP.CD']

How may I select both in one new dataframe or say 3 or 4? such that the rows are:

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

'SP.POP.TOTL'
'NY.GNP.PCAP.CD'

Thank you in advance

先感谢您

回答by MaxU

you may use .isin():

你可以使用.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