Python – Query Pandas Dataframe for a List of Columns

Import data (C:\Data\sample_data\california_housing_test.csv) from csv to dataframe

import pandas as pd
df = pd.read_csv('C:\Data\sample_data\california_housing_test.csv')
df

Select certain columns ("longitude","latitude","housing_median_age")

df[["longitude","latitude","housing_median_age"]]

Select certain columns ("longitude","latitude","housing_median_age"),

top n rows (5),

split code to multiple lines

df[["longitude","latitude","housing_median_age"]] \
.head(5)

Select certain columns ("longitude","latitude","housing_median_age"),

top n rows (5),

order by certain columns ("housing_median_age","latitude") asc | desc,

split code to multiple lines

df[["longitude","latitude","housing_median_age"]] \
.head(5) \
.sort_values(["housing_median_age","latitude"], ascending = (True,False))