~ 1 min read

Selecting Columns with Python Pandas

By: Adam Richardson
Share:

Intro

Perhaps the shortest content we will do with Python Pandas! Selecting columns is really simple!

Let’s take a look at our current Dataframe. I have reset and kept only the code we need, so we are in the same place.

Current Dataframe

dateestKeycapacityoccupancyroomsSoldavgRatesalesValuerevPAR
2022-12-2702890.7521735.977805.4927.008616
2022-12-2712030.357182.315844.0128.788227
2022-12-2722070.51106227.8324149.98116.666570
2022-12-273270.3710126.461264.6046.837037
2022-12-274200.8717191.573256.69162.834500

Selecting columns in Python pandas

OK, so we are doing some analysis on occupancy over time, and we will need the date, capacity,occupancy, and roomsSold columns. Let’s look at how we would select those.

occ_df = df[["date", "capacity", "occupancy", "roomsSold"]]

occ_df.head()

Output

datecapacityoccupancyroomsSold
2022-12-272890.75217
2022-12-272030.3571
2022-12-272070.51106
2022-12-27270.3710
2022-12-27200.8717

It’s really that simple. Note that we have created a new variable occ_df which is short for “occupancy dataframe”.

A good convention to follow, is to append _df to dataframe variable names so that it’s clear that’s what it is.

In the next tutorial, we’re going to add the relevant data columns to support our analysis.

Share:
Subscribe to our newsletter

Stay up to date with our latest content - No spam!

Related Posts