DataFrame 获取给定列的第一行
Suraj Joshi
2023年1月30日
2021年2月28日
本教程介绍了如何使用 Series.loc()
和 Series.iloc()
方法获得 DataFrame 中给定列的第一行。
我们将在本文中使用下面的 DataFrame 示例。
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)
输出:
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()
获取 DataFrame 中特定列的第一行
要使用 Series.loc()
从 Series 对象中获取某一行,我们只需将该行的索引名称作为参数传递给 Series.loc()
方法。
DataFrame 的每一列都是一个 Series 对象,我们可以使用 .loc()
方法来选择给定列的任何元素。
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)
输出:
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
它从 DataFrame student_df
的 Name
列中选择第一行并打印出来。我们传递第一行的索引,即 501
来选择第一行。
另外,我们也可以将第一行的索引和指定的列名作为参数传递给 loc()
方法,以提取 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)
输出:
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
它从 Name
列和第一行选择索引值 503
的值。
使用 Series.loc()
获取 DataFrame 中特定列的第一行
要使用 Series.iloc()
从 DataFrame 中获取特定行,我们将该行的整数索引作为参数传递给 Series.iloc()
方法。
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)
输出:
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
它从 DataFrame student_df
的 Name
列中选择第一行并打印出来。我们传递第一行的整数索引,即 0
,因为索引从 0
开始。
另外,我们也可以将第一行的整数索引和指定列的索引同时传递给 iloc()
方法作为参数,以提取 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)
输出:
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
它从 DataFrame 的第一行和第一列中选择数值。
Author: Suraj Joshi
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn