JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

DataFrame get the first row of a given column

Author:JIYIK Last Updated:2025/05/01 Views:

This tutorial explains how to use the Series.loc()and Series.iloc()methods to get the first row of a given column in a DataFrame.

We will use the following DataFrame example in this article.

import pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
    {
        "Name": ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
        "Gender": ["Female", "Male", "Male", "Female", "Female", "Male"],
        "Age": [17, 18, 17, 16, 18, 16],
    },
    index=roll_no,
)

print(student_df)

Output:

         Name Gender Age
501 Jennifer Female   17
502    Travis    Male   18
503       Bob    Male   17
504      Emma Female   16
505      Luna Female   18
506     Anish    Male   16

Series.loc()To get the first row of a specific column in a DataFrame, use

Series.loc()To get a row from a Series object using , we simply pass the index name of the row as an argument to Series.loc()the method.

Each column of a DataFrame is a Series object and we can use .loc()the method to select any element of a given column.

import pandas as pd

roll_no = [501, 502, 503, 504, 505, 506]

student_df = pd.DataFrame(
    {
        "Name": ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
        "Gender": ["Female", "Male", "Male", "Female", "Female", "Male"],
        "Age": [17, 18, 17, 16, 18, 16],
    },
    index=roll_no,
)

print("The DataFrame is:")
print(student_df, "\n")

first_row = student_df["Name"].loc[501]

print("First row from Name column is:")
print(first_row)

Output:

The DataFrame is:
         Name Gender Age
501 Jennifer Female   17
502    Travis    Male   18
503       Bob    Male   17
504      Emma Female   16
505      Luna Female   18
506     Anish    Male   16

First row from Name column is:
Jennifer

It selects the first row from the column student_dfof the DataFrame Nameand prints it. We pass the index of the first row, i.e., 501to select the first row.

Alternatively, we can also pass the first row index and the specified column name as arguments to loc()the method to extract the elements of the first row of the specified column in the DataFrame.

import pandas as pd

roll_no = [501, 502, 503, 504, 505, 506]

student_df = pd.DataFrame(
    {
        "Name": ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
        "Gender": ["Female", "Male", "Male", "Female", "Female", "Male"],
        "Age": [17, 18, 17, 16, 18, 16],
    },
    index=roll_no,
)

print("The DataFrame is:")
print(student_df, "\n")

first_name = student_df.loc[501, "Name"]

print("First row from Name column is:")
print(first_name)

Output:

The DataFrame is:
         Name Gender Age
501 Jennifer Female   17
502    Travis    Male   18
503       Bob    Male   17
504      Emma Female   16
505      Luna Female   18
506     Anish    Male   16

First row from Name column is:
Jennifer

It selects the value at index value from Namecolumn and first row 503.


Series.loc()To get the first row of a specific column in a DataFrame, use

Series.iloc()To get a specific row from a DataFrame using , we pass the integer index of that row as an argument to Series.iloc()the method.

import pandas as pd

roll_no = [501, 502, 503, 504, 505, 506]

student_df = pd.DataFrame(
    {
        "Name": ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
        "Gender": ["Female", "Male", "Male", "Female", "Female", "Male"],
        "Age": [17, 18, 17, 16, 18, 16],
    },
    index=roll_no,
)

print("The DataFrame is:")
print(student_df, "\n")

first_row = student_df["Name"].iloc[0]

print("First row from Name column is:")
print(first_row)

Output:

The DataFrame is:
         Name Gender Age
501 Jennifer Female   17
502    Travis    Male   18
503       Bob    Male   17
504      Emma Female   16
505      Luna Female   18
506     Anish    Male   16

First row from Name column is:
Jennifer

It selects the first row from the column student_dfof the DataFrame Nameand prints it. We pass the integer index of the first row, i.e. 0, , because the index starts 0at .

Alternatively, we can also pass both the integer index of the first row and the index of the specified column iloc()as arguments to the method to extract the entries of the first row of the specified column in the DataFrame.

import pandas as pd

roll_no = [501, 502, 503, 504, 505, 506]

student_df = pd.DataFrame(
    {
        "Name": ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
        "Gender": ["Female", "Male", "Male", "Female", "Female", "Male"],
        "Age": [17, 18, 17, 16, 18, 16],
    },
    index=roll_no,
)

print("The DataFrame is:")
print(student_df, "\n")

first_name = student_df.iloc[0, 0]

print("Name of student at first row is:")
print(first_name)

Output:

The DataFrame is:
         Name Gender Age
501 Jennifer Female   17
502    Travis    Male   18
503       Bob    Male   17
504      Emma Female   16
505      Luna Female   18
506     Anish    Male   16

Name of student at first row is:
Jennifer

It selects values ​​from the first row and first column of the DataFrame.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert Pandas to CSV without index

Publish Date:2025/05/01 Views:159 Category:Python

As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates

Convert Pandas DataFrame to Dictionary

Publish Date:2025/05/01 Views:197 Category:Python

This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan

Convert Pandas DataFrame columns to lists

Publish Date:2025/05/01 Views:191 Category:Python

When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m

Subtracting Two Columns in Pandas DataFrame

Publish Date:2025/05/01 Views:120 Category:Python

Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi

Dropping columns by index in Pandas DataFrame

Publish Date:2025/05/01 Views:99 Category:Python

DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns

Pandas Copy DataFrame

Publish Date:2025/05/01 Views:53 Category:Python

This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:

Pandas DataFrame.ix[] Function

Publish Date:2025/05/01 Views:168 Category:Python

Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice

Pandas DataFrame.describe() Function

Publish Date:2025/05/01 Views:120 Category:Python

Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial