Get the first row of Dataframe Pandas
This tutorial explains how to use the get_first_row pandas.DataFrame.iloc
attribute and pandas.DataFrame.head()
get_first_row method from a Pandas DataFrame.
We will use the following DataFrame in the following example to explain how to get the first row from a Pandas DataFrame.
import pandas as pd
df = pd.DataFrame(
{
"C_1": ["A", "B", "C", "D"],
"C_2": [40, 34, 38, 45],
"C_3": [430, 980, 200, 350],
}
)
print(df)
Output:
C_1 C_2 C_3
0 A 40 430
1 B 34 980
2 C 38 200
3 D 45 350
pandas.DataFrame.iloc
Get the first row of a Pandas DataFrame using the
import pandas as pd
df = pd.DataFrame(
{
"C_1": ["A", "B", "C", "D"],
"C_2": [40, 34, 38, 45],
"C_3": [430, 980, 200, 350],
}
)
row_1 = df.iloc[0]
print("The DataFrame is:")
print(df, "\n")
print("The First Row of the DataFrame is:")
print(row_1)
Output:
The DataFrame is:
C_1 C_2 C_3
0 A 40 430
1 B 34 980
2 C 38 200
3 D 45 350
The First Row of the DataFrame is:
C_1 A
C_2 40
C_3 430
Name: 0, dtype: object
It displays df
the first row of the DataFrame. To select the first row, we use the default index of the first row, i.e., 0
and the DataFrame's iloc
attribute.
pandas.DataFrame.head()
Get the first row from a Pandas DataFrame using
pandas.DataFrame.head()
The method returns a DataFrame containing the top 5 rows of the DataFrame. We can also pass a number as a parameter to pandas.DataFrame.head()
the method, representing the number of top rows to select. We can pass 1 as a parameter to pandas.DataFrame.head()
the method to select only the first row of the DataFrame.
import pandas as pd
df = pd.DataFrame(
{
"C_1": ["A", "B", "C", "D"],
"C_2": [40, 34, 38, 45],
"C_3": [430, 980, 200, 350],
}
)
row_1 = df.head(1)
print("The DataFrame is:")
print(df, "\n")
print("The First Row of the DataFrame is:")
print(row_1)
Output:
The DataFrame is:
C_1 C_2 C_3
0 A 40 430
1 B 34 980
2 C 38 200
3 D 45 350
The First Row of the DataFrame is:
C_1 C_2 C_3
0 A 40 430
Get the first row from a Pandas DataFrame based on a specified condition
To extract the first row from a DataFrame that satisfies a specified condition, we first filter the rows that satisfy the specified condition and then select the first row from the filtered DataFrame using the method discussed above.
import pandas as pd
df = pd.DataFrame(
{
"C_1": ["A", "B", "C", "D"],
"C_2": [40, 34, 38, 45],
"C_3": [430, 980, 500, 350],
}
)
filtered_df = df[(df.C_2 < 40) & (df.C_3 > 450)]
row_1_filtered = filtered_df.head(1)
print("The DataFrame is:")
print(df, "\n")
print("The Filtered DataFrame is:")
print(filtered_df, "\n")
print("The First Row with C_2 less than 45 and C_3 greater than 450 is:")
print(row_1_filtered)
Output:
The DataFrame is:
C_1 C_2 C_3
0 A 40 430
1 B 34 980
2 C 38 500
3 D 45 350
The Filtered DataFrame is:
C_1 C_2 C_3
1 B 34 980
2 C 38 500
The First Row with C_2 less than 45 and C_3 greater than 450 is:
C_1 C_2 C_3
1 B 34 980
It will display the first row where the column C_2
value is less than 45 and C_3
the column value is greater than 450.
We can also use query()
the method to filter the rows in the DataFrame.
import pandas as pd
df = pd.DataFrame(
{
"C_1": ["A", "B", "C", "D"],
"C_2": [40, 34, 38, 45],
"C_3": [430, 980, 500, 350],
}
)
filtered_df = df.query("(C_2 < 40) & (C_3 > 450)")
row_1_filtered = filtered_df.head(1)
print("The DataFrame is:")
print(df, "\n")
print("The Filtered DataFrame is:")
print(filtered_df, "\n")
print("The First Row with C_2 less than 45 and C_3 greater than 450 is:")
print(row_1_filtered)
Output:
The DataFrame is:
C_1 C_2 C_3
0 A 40 430
1 B 34 980
2 C 38 500
3 D 45 350
The Filtered DataFrame is:
C_1 C_2 C_3
1 B 34 980
2 C 38 500
The First Row with C_2 less than 45 and C_3 greater than 450 is:
C_1 C_2 C_3
1 B 34 980
It will use query()
the method to filter all rows where C_2
the column value is less than 45 and the column C_3
value is greater than 450, and then use head()
the method filtered_df
to select the first row from .
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.
Related Articles
Finding the installed version of Pandas
Publish Date:2025/04/12 Views:190 Category:Python
-
Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc
KeyError in Pandas
Publish Date:2025/04/12 Views:81 Category:Python
-
This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest
Grouping and Sorting in Pandas
Publish Date:2025/04/12 Views:90 Category:Python
-
This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies
Plotting Line Graph with Data Points in Pandas
Publish Date:2025/04/12 Views:65 Category:Python
-
Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize
Converting Timedelta to Int in Pandas
Publish Date:2025/04/12 Views:123 Category:Python
-
This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .
Pandas fill NaN values
Publish Date:2025/04/12 Views:93 Category:Python
-
This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50
Pandas Convert String to Number
Publish Date:2025/04/12 Views:147 Category:Python
-
This tutorial explains how to pandas.to_numeric() convert string values of a Pandas DataFrame into numeric type using the method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 , 103 , 343 , 565 ], "Name" :
How to Change the Data Type of a Column in Pandas
Publish Date:2025/04/12 Views:139 Category:Python
-
We will look at methods for changing the data type of columns in a Pandas Dataframe, as well as options like to_numaric , , as_type and infer_objects . We will also discuss how to to_numaric use downcasting the option in . to_numeric Method
Pandas Drop Duplicate Rows in DataFrame
Publish Date:2025/04/12 Views:75 Category:Python
-
This tutorial explains how to DataFrame.drop_duplicates() remove all duplicate rows from a Pandas DataFrame using the remove_by method. DataFrame.drop_duplicates() grammar DataFrame . drop_duplicates(subset = None , keep = "first" , inplace