In this article, we will discuss different ways to select first row of dataframe in pandas.
Select & print first row of dataframe using iloc[]
Before diving deep into the solution, let’s first have a look at the dataframe’s iloc.
Overview of dataframe iloc[]
Pandas provides a dataframe attribute iloc[] for location based indexing i.e.
dataframe.iloc[row_section, col_section] dataframe.iloc[row_section]
Arguments if iloc[]
- row_section: It can be,
- A row number
- A list of row numbers
- A range of row numbers – start:end i.e. from start to end-1.
- column_section: It can be
- A column number
- A column of row numbers
- A range of column numbers – start:end i.e. from start to end-1.
It selects the subset of dataframe based on the row/column numbers specified in these row & column sections.
Get first row of pandas dataframe as a series
To select the first row of dataframe using iloc[], we can just skip the column section and in row section pass the 1 as row number. It will select the first row i.e. row at index 0,
df.iloc[0]
We got the first row of dataframe as a series object.
Get first row of pandas dataframe as a dataframe
If you want the first row of dataframe as a dataframe object then you can provide the range i.e.[:1], instead of direct number i.e.
df.iloc[:1]
It will select the rows from number 0 to 1 and return the first row of dataframe as a dataframe object.
Learn More about iloc[] and loc[] properties of Dataframe,
Complete example:
Let’s see an example, where we will select and print the first row of dataframe using both the specified ways,
import pandas as pd # List of Tuples employees = [('Jack', 34, 'Sydney', 5) , ('Shaun', 31, 'Delhi' , 7) , ('Shachin', 16, 'London', 3) , ('Eva', 41, 'Delhi' , 4)] # Create a DataFrame object df = pd.DataFrame( employees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) # Select first row of the dataframe as a series first_row = df.iloc[0] print("First row Of Dataframe: ") print(first_row) # Select first row of the dataframe as a dataframe object first_row_df = df.iloc[:1] print("First row Of Dataframe: ") print(first_row_df)
Output:
Contents of the Dataframe : Name Age City Experience 0 Jack 34 Sydney 5 1 Shaun 31 Delhi 7 2 Shachin 16 London 3 3 Eva 41 Delhi 4 First row Of Dataframe: Name Jack Age 34 City Sydney Experience 5 Name: 0, dtype: object First row Of Dataframe: Name Age City Experience 0 Jack 34 Sydney 5
At, first We selected the first row of dataframe as a series object & printed it. After that we selected the first row as a dataframe and then again printed it.
Select & print first row of dataframe using head()
In Pandas, the dataframe provides a function head(n). It returns the first n rows of dataframe. We can use this head() function to get only the first row of the dataframe,
df.head(1)
It will return the first row of dataframe as a dataframe object.
Let’s see a complete example,
import pandas as pd # List of Tuples employees = [('Jack', 34, 'Sydney', 5) , ('Shaun', 31, 'Delhi' , 7) , ('Shachin', 16, 'London', 3) , ('Eva', 41, 'Delhi' , 4)] # Create a DataFrame object df = pd.DataFrame( employees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) # Select first row of the dataframe first_row = df.head(1) print("First row Of Dataframe: ") print(first_row)
Output:
Contents of the Dataframe : Name Age City Experience 0 Jack 34 Sydney 5 1 Shaun 31 Delhi 7 2 Shachin 16 London 3 3 Eva 41 Delhi 4 First row Of Dataframe: Name Age City Experience 0 Jack 34 Sydney 5
Using the head() function, we fetched the first row of dataframe as a dataframe and then just printed it.
Get first row of pandas dataframe as list
We can select the first row of dataframe using df.iloc[0]. It will give us a series object and then using the series’s tolist() function, we can get a list containing the contents of first row of dataframe. For example,
import pandas as pd # List of Tuples employees = [('Jack', 34, 'Sydney', 5) , ('Shaun', 31, 'Delhi' , 7) , ('Shachin', 16, 'London', 3) , ('Eva', 41, 'Delhi' , 4)] # Create a DataFrame object df = pd.DataFrame( employees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) # Select first row as list first_row = df.iloc[0].tolist() print("First row Of Dataframe: ") print(first_row)
Output:
Contents of the Dataframe : Name Age City Experience 0 Jack 34 Sydney 5 1 Shaun 31 Delhi 7 2 Shachin 16 London 3 3 Eva 41 Delhi 4 First row Of Dataframe: ['Jack', 34, 'Sydney', 5]
Summary:
We learned about different ways to get the first row of dataframe.
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.