[Python] Code Example 3 - Filtering Pandas DataFrame




[Source Code]

# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Import numpy, you'll need this
import numpy as np

# Create medium: observations with cars_per_cap between 100 and 500
cpc = cars['cars_per_cap']
between = np.logical_and(cpc > 100, cpc < 500)
medium = cars[between]

# Print medium
print(medium)


[Output]==========================================

    cars_per_cap country  drives_right

RU           200  Russia          True


[Reference]========================================
In [2]: cars
Out[2]: 
     cars_per_cap        country  drives_right
US            809  United States          True
AUS           731      Australia         False
JAP           588          Japan         False
IN             18          India         False
RU            200         Russia          True
MOR            70        Morocco          True

EG             45          Egypt          True

Comments

Popular posts from this blog

[Python] Code Example 1 - Dictionary

[Python] Code Example 2 - CSV to DataFrame