Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This is not a repetitive question, yet similar to

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

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

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

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:

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

Thank you in advance

like image 967
rsc05 Avatar asked Apr 04 '16 18:04

rsc05


People also ask

How do you find duplicate rows in pandas based on multiple columns?

Find Duplicate Rows based on all columns To find & select the duplicate all rows based on all columns call the Daraframe. duplicate() without any subset argument. It will return a Boolean series with True at the place of each duplicated rows except their first occurrence (default value of keep argument is 'first').

How do you drop duplicate rows in pandas based on a column?

Use DataFrame. drop_duplicates() to Drop Duplicate and Keep First Rows. You can use DataFrame. drop_duplicates() without any arguments to drop rows with the same values on all columns.


1 Answers

you may use .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
like image 168
MaxU - stop WAR against UA Avatar answered Oct 02 '22 19:10

MaxU - stop WAR against UA