Get First value of a Column in Pandas DataFrame

This article will discuss different ways to get the first value a column in a Pandas Dataframe. In all the solutions, we will first select the Column by either column name or index position; then, we will see different techniques to get and set the first value of that Column.

Table of Contents

A DataFrame is a data structure offered by the Pandas module in Python. It stores the data in tabular format, i.e., in rows and columns. Let’s create a DataFrame from a list of tuples,

import pandas as pd

# List of Tuples
students = [('jack',    34, 'Sydney',   'Australia', 0),
            ('Riti',    30, 'Delhi',    'India',     0),
            ('Vikas',   31, 'Mumbai',   'India',     0),
            ('Neelu',   32, 'Bangalore','India',     0),
            ('John',    16, 'New York',  'US',       0),
            ('Mike',    17, 'las vegas', 'US',       0)]

# Create a DataFrame object
df = pd.DataFrame( students,
                   columns=['Name', 'Age', 'City', 'Country', 'Budget'],
                   index=['a', 'b', 'c', 'd', 'e', 'f'])

print(df)

Output

    Name  Age       City    Country  Budget
a   jack   34     Sydney  Australia       0
b   Riti   30      Delhi      India       0
c  Vikas   31     Mumbai      India       0
d  Neelu   32  Bangalore      India       0
e   John   16   New York         US       0
f   Mike   17  las vegas         US       0

Now let’s see how to get the first value of a specific column of this DataFrame,

Advertisements

Get the first value of a column based on the column name

Suppose we know the column name and want to fetch the first value of that Column. We can do that using two techniques. Let’s see them one by one,

Get the first value of a column using iat[]

Select the Column of the DataFrame as a Series object, based on the column name. Then call the iat[0] attribute on that Series object to get the first value of that Column. For example,

# Get first value of column 'City'
first_value = df['City'].iat[0]

print(first_value)

Output:

Sydney

Here we fetched the first value of the column ‘City’ from the DataFrame. The iat[0] returns the reference of the first value of the Series. We can use this to change the first value of the Column too. For example,

# Change the First value of column 'City'
df['City'].iat[0] = 'Mumbai'

# Display the DataFrame
print(df)

Output:

    Name  Age       City    Country  Budget
a   jack   34     Mumbai  Australia       0
b   Riti   30      Delhi      India       0
c  Vikas   31     Mumbai      India       0
d  Neelu   32  Bangalore      India       0
e   John   16   New York         US       0
f   Mike   17  las vegas         US       0

Here, we changed the first value of column ‘City’ to Mumbai.

Get the first value of a column using iloc[]

In Pandas, the DataFrame provides a property iloc[]. In the iloc[row_number, column_number], we need to pass the row and column index positions, and it fetches the cell value based on that. But we have the column name instead of the column index position. So we need to get the column index from the column name and then use the iloc[] property with row value 0 to get the first value of the Column. For example,

import pandas as pd

# List of Tuples
students = [('jack',    34, 'Sydney',   'Australia', 0),
            ('Riti',    30, 'Delhi',    'India',     0),
            ('Vikas',   31, 'Mumbai',   'India',     0),
            ('Neelu',   32, 'Bangalore','India',     0),
            ('John',    16, 'New York',  'US',       0),
            ('Mike',    17, 'las vegas', 'US',       0)]

# Create a DataFrame object
df = pd.DataFrame( students,
                   columns=['Name', 'Age', 'City', 'Country', 'Budget'],
                   index=['a', 'b', 'c', 'd', 'e', 'f'])

print(df)

# Get first value of column 'City'
first_value = df.iloc[0, df.columns.get_loc('City')]

print(first_value)

Output:

    Name  Age       City    Country  Budget
a   jack   34     Sydney  Australia       0
b   Riti   30      Delhi      India       0
c  Vikas   31     Mumbai      India       0
d  Neelu   32  Bangalore      India       0
e   John   16   New York         US       0
f   Mike   17  las vegas         US       0


Sydney

Here we fetched the first value of the column ‘City’ from the DataFrame.

Using the get_loc() function, we first fetched the column number from column name and then using passed that to iloc[] property of the DataFrame with row value 0. The iloc[], returned the reference of the first value of the Column. We can use this to change the first value of the Column too. For example,

# Change the First value of column 'City'
df.iloc[0, df.columns.get_loc('City')] = 'Tokyo'

# Display the DataFrame
print(df)

Output:

    Name  Age       City    Country  Budget
a   jack   34      Tokyo  Australia       0
b   Riti   30      Delhi      India       0
c  Vikas   31     Mumbai      India       0
d  Neelu   32  Bangalore      India       0
e   John   16   New York         US       0
f   Mike   17  las vegas         US       0

Here, we changed the first value of column ‘City’ to Tokyo.

Get the first value of a column based on column index position

In Pandas, the DataFrame provides a property iloc[]. In the iloc[row_number, column_number], we need to pass the row and column index positions, and it fetches the cell value based on that.

As we already have the column index position, we can directly use that and pass 0 as the row number to get the first value of the Column. For example,

import pandas as pd

# List of Tuples
students = [('jack',    34, 'Sydney',   'Australia', 0),
            ('Riti',    30, 'Delhi',    'India',     0),
            ('Vikas',   31, 'Mumbai',   'India',     0),
            ('Neelu',   32, 'Bangalore','India',     0),
            ('John',    16, 'New York',  'US',       0),
            ('Mike',    17, 'las vegas', 'US',       0)]

# Create a DataFrame object
df = pd.DataFrame( students,
                   columns=['Name', 'Age', 'City', 'Country', 'Budget'],
                   index=['a', 'b', 'c', 'd', 'e', 'f'])

print(df)


column_index = 2
# Get first value of column index 2
first_value = df.iloc[0, column_index]

print(first_value)

Output:

    Name  Age       City    Country  Budget
a   jack   34     Sydney  Australia       0
b   Riti   30      Delhi      India       0
c  Vikas   31     Mumbai      India       0
d  Neelu   32  Bangalore      India       0
e   John   16   New York         US       0
f   Mike   17  las vegas         US       0


Sydney

Here we fetched the first value of the column ‘City’ from the DataFrame. The iloc[] returns the reference of the first value of the Column. We can use this to change the first value of the Column too. For example,

column_index = 2
# Change the First value of column index 2
df.iloc[0, column_index] = 'Yokohama'

# Display the DataFrame
print(df)

Output:

    Name  Age       City    Country  Budget
a   jack   34   Yokohama  Australia       0
b   Riti   30      Delhi      India       0
c  Vikas   31     Mumbai      India       0
d  Neelu   32  Bangalore      India       0
e   John   16   New York         US       0
f   Mike   17  las vegas         US       0

Here, we changed the first value of column ‘City’ to Yokohama.

Summary

We learned about different ways to get and set a column’s first value, either by column name or index position.

Pandas Tutorials -Learn Data Analysis with Python

   

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.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top