Get first row value of a given column in Pandas Dataframe

0

To get the first row values of a given Column of a pandas DataFrame:

  1. Use the df.iloc[0] method (Slowest)
  2. Use the df.columns.get_loc() method
  3. Use the df.column.values[0] mehtod (Fastest)
  4. Use the df.iat() method

Get first row of a Column of a DataFrame Using the df.iloc[0] method

To get the first-row value of a column use the df.iloc[0] method. The df.iloc[0] method is a built-in method in pandas library that find the location of a row. See the following example:

import pandas as pd 
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
                   'num_wings': [2, 0, 0, 0],
                   'num_specimen_seen': [10, 2, 1, 8]},
                  index=['falcon', 'dog', 'spider', 'fish'])

first_row = df.iloc[0]
print(first_row)

# Output πŸ‘‡
# num_legs              2
# num_wings             2
# num_specimen_seen    10
# Name: falcon, dtype: int64

Now to check the value of a row of a given column in a dataframe Use the df[column_name].iloc[0] method. This will return the value of the exact row of that column in the DataFrame.

import pandas as pd 
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
                   'num_wings': [2, 0, 0, 0],
                   'num_specimen_seen': [10, 2, 1, 8]},
                  index=['falcon', 'dog', 'spider', 'fish'])

first_row = df['num_legs'].iloc[0]
print(first_row)

# Output πŸ‘‡
# 2


Get the first value of a given column of a DataFrame using get_loc() method

The df.column.get_loc() funciton is also used to find the first row value of a given column in pandas DataFrame. You have to use the column name and the get_loc() function.

import pandas as pd 
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
                   'num_wings': [2, 0, 0, 0],
                   'num_specimen_seen': [10, 2, 1, 8]},
                  index=['falcon', 'dog', 'spider', 'fish'])

column_e = df.columns.get_loc('num_legs')
first_row = df.iloc[0,column_e]
print(first_row)

# Output πŸ‘‡
# 2


Get the value of a row of a given column using the df.values method

The df.values method is used to get the values of a column. so we can retrieve the value of the first row of that column in the dataframe. This is an easy-to-use method. An example is given:

import pandas as pd 
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
                   'num_wings': [2, 0, 0, 0],
                   'num_specimen_seen': [10, 2, 1, 8]},
                  index=['falcon', 'dog', 'spider', 'fish'])


first_row = df['num_legs'].values[0]
print(first_row)

# Output πŸ‘‡
# 2


Get the value of the first row of a given column in Pandas DataFrame

We can also use the iat() function to get the value of the first row of a given column of a DataFrame. Check the following Example:

import pandas as pd 
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
                   'num_wings': [2, 0, 0, 0],
                   'num_specimen_seen': [10, 2, 1, 8]},
                  index=['falcon', 'dog', 'spider', 'fish'])


first_row = df['num_legs'].iat[0]
print(first_row)

# Output πŸ‘‡
# 2


Conclusion: The Fastest Method to get the value of the First row of a column in the DataFrame

Upon the below analysis, we have found out the fastest method to find the value of the first row of a given column in the pandas dataframe was to use the df['num_legs'].values[0] method. Which is the Third method in the list. To my surprise, a lot of people are using the iloc() method.

import timeit
import pandas as pd

import pandas as pd 
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
                   'num_wings': [2, 0, 0, 0],
                   'num_specimen_seen': [10, 2, 1, 8]},
                  index=['falcon', 'dog', 'spider', 'fish'])

def first_m():
    return  df['num_legs'].iloc[0]
    
def second_m():
    column_e = df.columns.get_loc('num_legs')
    return  df.iloc[0,column_e]
    
def third_m():
    return df['num_legs'].values[0]

def fourth_m():
    return df['num_legs'].iat[0]


for func in [first_m,second_m, third_m, fourth_m]:
    print(f"{func.__name__} Time Took: ",  timeit.timeit(stmt=func, number=100000))
    
# first_m Time Took:  1.5019117000047117
# second_m Time Took:  3.3820728000137024
# third_m Time Took:  0.547329599969089
# fourth_m Time Took:  0.6441925999824889



Have a Chat With Me!

I hope you liked this Python TutorialπŸ’—. Now, Don't hesitate to leave a message at haxratali0@gamil.com. I respond to every mail I receive. I will be more than happy to chat with you.

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !