Select rows from pandas DataFrame

Selecting the rows whose column values are equal to the given value in pandas:

import pandas

df = pandas.read_csv('SUB-IP-EST2019-ANNRNK.csv')

df2 = df.loc[df['City'] == 'New York city, New York']

print(df2)
#                       City       2010  ...       2018       2019
# 0  New York city, New York  8,190,209  ...  8,390,081  8,336,817

The data is the annual populations of the US cities (2010-2019).

Select multiple rows

import pandas

df = pandas.read_csv('SUB-IP-EST2019-ANNRNK.csv')

cities = ['New York city, New York', 'San Jose city, California']

df2 = df.loc[df['City'].isin(cities)]

print(df2)
#                         City       2010  ...       2018       2019
# 0    New York city, New York  8,190,209  ...  8,390,081  8,336,817
# 9  San Jose city, California    954,940  ...  1,028,020  1,021,795

To select multiple rows, use isin().

Select cities in Nevada

import pandas

df = pandas.read_csv('SUB-IP-EST2019-ANNRNK.csv')

df2 = df.loc[df['City'].str.contains('Nevada')]

print(df2)
#                              City     2010     2011  ...     2017     2018     2019
# 26         Las Vegas city, Nevada  584,576  586,606  ...  635,262  643,228  651,319
# 60         Henderson city, Nevada  257,452  258,752  ...  299,314  309,518  320,189
# 84              Reno city, Nevada  225,652  227,061  ...  246,615  250,146  255,601
# 88   North Las Vegas city, Nevada  216,836  218,291  ...  240,504  245,349  251,974
# 293           Sparks city, Nevada   91,237   91,616  ...  100,048  103,979  105,006
# 681           Carson City, Nevada   54,981   54,676  ...   54,532   55,202   55,916

str.contains('Nevada') is selecting the values that contains Nevada.

Select cities whose names don't contain 'city'

import pandas

df = pandas.read_csv('SUB-IP-EST2019-ANNRNK.csv')

df2 = df.loc[~df['City'].str.contains('city,')]

print(df2)
#                                                   City  ...     2019
# 16                Indianapolis city (balance), Indiana  ...  876,384
# 22   Nashville-Davidson metropolitan government (ba...  ...  670,820
# 28   Louisville/Jefferson County metro government (...  ...  617,638
# 55                          Urban Honolulu CDP, Hawaii  ...  345,064
# 59            Lexington-Fayette urban county, Kentucky  ...  323,152
# 68                      Anchorage municipality, Alaska  ...  288,000
# 86                               Gilbert town, Arizona  ...  254,114
# 122  Augusta-Richmond County consolidated governmen...  ...  197,888
# 153                          Cary town, North Carolina  ...  170,282
# 167                         Macon-Bibb County, Georgia  ...  153,159

The prefix ~ of df['City'] means the negation or excluding the rows satisfying the given conditions.

Source

Source: City and Town Population Totals: 2010-2019 (United States Census Bureau)

The data used in this article is a part of the original file.

pandas DataFrame

pandas tutorial