In this article, we will discuss different ways to get or select the first column of dataframe as a series or list object.
Table of Contents
- Use iloc[] to select first column of pandas dataframe.
- Use [] to select first column of pandas dataframe.
- Use head() to select first column of pandas dataframe.
- Get first column of pandas dataframe as list on python.
There are different ways to select the first column of this dataframe. Let’s discuss them one by one,
Use iloc[] to select first column of pandas dataframe
In Pandas, the Dataframe provides an attribute iloc[], to select a portion of the dataframe using position based indexing. This selected portion can be few columns or rows . We can use this attribute to select only first column of the dataframe. For example,
# Select first column of the dataframe as a series first_column = df.iloc[:, 0]
We selected a portion of dataframe as a series object, that included all rows, but only first column of the dataframe.
How did it work?
The syntax of dataframe.iloc[] is like,
df.iloc[row_start:row_end , col_start, col_end]
Arguments:
- row_start: The row index/position from where it should start selection. Default is 0.
- row_end: The row index/position from where it should end the selection i.e. select till row_end-1. Default is till the last row of the dataframe.
- col_start: The column index/position from where it should start selection. Default is 0.
- col_end: The column index/position from where it should end the selection i.e. select till end-1. Default is till the last column of the dataframe.
It returns a portion of the dataframe that includes rows from row_start to row_end-1 and columns from col_start to col_end-1.
To select the first column of dataframe select from column index 0 till 1 i.e (:1) and select all rows using default values (:),
# Select first column of the dataframe as a dataframe first_column = df.iloc[: , :1]
We provided the range to select the columns from 0 position till 1 to select the first column, therefore it returned a dataframe. If you want to select the first column as a series object then just pass the 0 instead of range. For example,
# Select first column of the dataframe as a series first_column = df.iloc[:, 0]
Checkout complete example to select first column of dataframe using iloc,
import pandas as pd # List of Tuples empoyees = [('Jack', 34, 'Sydney', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aadi', 16, 'London', 11) , ('Mark', 41, 'Delhi' , 12)] # Create a DataFrame object df = pd.DataFrame( empoyees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) # Select first column of the dataframe as a dataframe object first_column = df.iloc[: , :1] print("First Column Of Dataframe: ") print(first_column) print("Type: " , type(first_column)) # Select first column of the dataframe as a series first_column = df.iloc[:, 0] print("First Column Of Dataframe: ") print(first_column) print("Type: " , type(first_column))
Output:
Contents of the Dataframe : Name Age City Experience 0 Jack 34 Sydney 5 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12 First Column Of Dataframe: Name 0 Jack 1 Riti 2 Aadi 3 Mark Type: <class 'pandas.core.frame.DataFrame'> First Column Of Dataframe: 0 Jack 1 Riti 2 Aadi 3 Mark Name: Name, dtype: object Type: <class 'pandas.core.series.Series'>
We selected the first column of dataframe.
Learn more,
Select first column of pandas dataframe using []
We can fetch the column names of dataframe as a sequence and then select the first column name. Then using that column name, we can select the first column of dataframe as a series object using subscript operator i.e. []. For example,
# Select first column of the dataframe first_column = df[df.columns[0]] print("First Column Of Dataframe: ") print(first_column) print("Type: " , type(first_column))
Output:
First Column Of Dataframe: 0 Jack 1 Riti 2 Aadi 3 Mark Name: Name, dtype: object Type: <class 'pandas.core.series.Series'>
Use head() to select the first column of pandas dataframe
We can use the dataframe.T attribute to get a transposed view of the dataframe and then call the head(1) function on that view to select the first row i.e. the first column of original dataframe. Then transpose back that series object to have the column contents as a dataframe object. For example,
# Select first column of the dataframe first_column = df.T.head(1).T print("First Column Of Dataframe: ") print(first_column) print("Type: " , type(first_column))
Output:
First Column Of Dataframe: Name 0 Jack 1 Riti 2 Aadi 3 Mark Type: <class 'pandas.core.frame.DataFrame'>
It returned the first column of dataframe as a dataframe object.
Pandas: Get first column of dataframe as list
Select the first column of dataframe as a series object using iloc[:, 0] and then call the tolist() function on the series object. It will return the first column of dataframe as a list object. For example,
# Select first Column first_column = df.iloc[:, 0].tolist() print("First Column Of Dataframe: ") print(first_column) print("Type: " , type(first_column))
Output:
First Column Of Dataframe: ['Jack', 'Riti', 'Aadi', 'Mark'] Type: <class 'list'>
It returned the first column of dataframe as a list.
Summary
We learned different ways to get the first column of a dataframe as a series or list object in python.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.