Sample DataFrame
In this article, we will use a sample DataFrame as shown in the example code below:
import pandas as pd
df = pd.DataFrame({
'products': ['Product1', 'Product2', 'Product3'],
'price': [100.9, 10.33, 12.00],
'quantity': [100, 10, 34]},
index=[1,2,3]
)
df
The resulting DataFrame is as shown below:
Feel free to use your dataset for better understanding.
Select Columns by Index
The first method we will discuss is selecting columns by their indices. For that, we can use the iloc method.
The syntax is expressed below:
For example, to get the first and second columns (including all rows), we can do the following:
The above should return:
Select Column by Index Range
We can also select multiple columns by specifying their index range. For example, in our sample DataFrame, we can select the columns from index 0 to 3 as shown:
This should return the entire DataFrame as shown:
Select Column by Name
To select columns by name, we can use the syntax shown below:
An example is as shown below:
This should return:
Select Columns Between Column Names
You may need to select columns between two column names in some instances. For that, we can use the syntax shown below:
In our example DataFrame, we can do:
This should return a DataFrame as shown:
Closing
This article taught us how to use select columns in a Pandas DataFrame using their index positions, index range, and column names.
Thanks for reading!!
from https://ift.tt/hzarqwp
0 Comments