In this tutorial, we will know how to get the first column of a Pandas Dataframe. Later, we will understand the same with the help of a few examples.
How to get the first column of a Dataframe using Pandas?
We can get the first column of a Dataframe in the following ways:
- by using the column index
- by using column name/label
1. Fetching the first column by index :
In case we are not aware of the first column name then we can always access the first column by the index, using the iloc
property.
Syntax: dataFrameName.iloc[:,0]
Note: Columns in a pandas dataframe are indexed starting from 0.
2. Fetching the first column by its name or label :
In order to access the first column by the column name or label, we can use the loc
property which allows you to access rows and columns of a dataframe by their labels
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Syntax: dataFrameName.loc[:,”firstColumnName”]
Examples:
We will now look at a few examples for a better understanding.
But before that, we will create a pandas dataframe that we will be using throughout this tutorial using the following command:
import pandas as pd # employee data data = { "Name": ["Jim", "Dwight", "Angela", "Tobi"], "Age": [26, 28, 27, 32], "Department": ["Sales", "Sales", "Accounting", "HR"] } # create pandas dataframe df = pd.DataFrame(data) # displays dataframe df
Output:
Example 1: Access the first column using the column index
Let’s access the first column of the dataframe created above using its column index (column position). Columns are indexed starting from 0, so, the index of the first column is 0.
# get the first column of the dataframe df.iloc[:, 0]
Output:
0 Jim 1 Dwight 2 Angela 3 Tobi Name: Name, dtype: object
Example 2: Access the first column using the column name
Alternatively, you can access any column using its name. Now if you know the name of the first column, you can directly access it using the loc
property.
# get the column using its name df.loc[:, "Name"]
Output:
0 Jim 1 Dwight 2 Angela 3 Tobi Name: Name, dtype: object
You can also directly use the column name using the square bracket notation of the dataframe to access it.
# get the column using its name df["Name"]
Output:
0 Jim 1 Dwight 2 Angela 3 Tobi Name: Name, dtype: object
We get the same result as above.
Summary:
In this tutorial, we looked at how to access the first column in a Pandas dataframe. The following are the key takeaways –
- Use the
iloc
property to access the first column using its index, which is 0. - Use the
loc
property to access the first column using its name (or label). Alternatively, you can directly access the column by its name using the[]
notation.
You might also be interested in –
- Check if a Column Exists in a Pandas DataFrame
- Check if Pandas DataFrame column has object dtype
- Most frequent value in a Pandas Column
- Split Pandas column of lists into multiple columns
- Pandas – Set Column as Index (With Examples)
- Pandas – Check if a column is all one value
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.