如何获取 Pandas DataFrame 的第一行?
在本文中,我们将讨论如何获取 pandas 数据框的第一行
方法一:使用 iloc[]
此方法用于通过使用行号来访问行。我们可以通过使用 0 索引来获取第一行。
语法:
dataframe.iloc[0]
其中数据框是输入数据框
我们还可以提供范围索引。
句法:
dataframe.iloc[:1]
此处的行将从头开始提取,直到 : 右侧之后提到的索引 -1。
示例:使用 iloc[]函数获取数据帧第一行的Python代码
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using row position
print(data.iloc[0])
print("---------------")
# get first row using slice operator
print(data.iloc[:1])
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using row position
print(data['name'].iloc[0])
print("---------------")
# get first row using slice operator
print(data['subjects'].iloc[:1])
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using head() function
print(data.head(1))
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using head() function
print(data['id'].head(1))
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using loc() function
data.loc[data.index[0]]
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using loc() function
print(data.values[:1])
# get first row using loc() function
print(data.values[:1])
# get particular column
print(data['name'].values[:1])
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using iat() function of
# first row 2 nd column
print(data.iat[0, 1])
# get first row using iat() function of
# first row 1 st column
print(data.iat[0, 0])
# get first row using iat() function of
# first row 3 rd column
print(data.iat[0, 2])
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using iat() function of
# first row 2 nd column
print(data.at[0, 'name'])
# get first row using iat() function of
# first row 1 st column
print(data.at[0, 'id'])
# get first row using iat() function of
# first row 3 rd column
print(data.at[0, 'subjects'])
输出:
id 7058
name sravan
subjects java
Name: 0, dtype: object
---------------
id name subjects
0 7058 sravan java
示例 2:获取特定列的第一行
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using row position
print(data['name'].iloc[0])
print("---------------")
# get first row using slice operator
print(data['subjects'].iloc[:1])
输出:
sravan
---------------
0 java
Name: subjects, dtype: object
方法二:使用 head()函数
此函数将默认返回数据帧的前 5 行。要仅获取第一行,我们必须指定 1
语法:
dataframe.head(1)
示例 1 :获取数据集第一行的程序
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using head() function
print(data.head(1))
输出:
id name subjects
0 7058 sravan java
示例 2:获取特定列的第一行
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using head() function
print(data['id'].head(1))
输出:
0 7058
Name: id, dtype: int64
方法 3:使用 loc()函数
此方法用于获取具有索引函数的第一行。
语法:
dataframe.loc[dataframe.index[0]]
在哪里,
- 数据框是输入数据框
- index 是获取第一行的函数
示例:获取数据集第一行的程序
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using loc() function
data.loc[data.index[0]]
输出:
id 7058
name sravan
subjects java
Name: 0, dtype: object
方法 4:使用 values()函数
这将以数组的形式返回第一行。工作原理类似于 iloc()。
语法:
dataframe.values[0]
dataframe.values[:1]
示例:获取数据集第一行的程序
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using loc() function
print(data.values[:1])
# get first row using loc() function
print(data.values[:1])
# get particular column
print(data['name'].values[:1])
输出:
[[7058 'sravan' 'java']]
[[7058 'sravan' 'java']]
['sravan']
方法 5:使用 iat[]函数
此函数采用行和列索引来显示数据框中的数据
语法:
dataframe.iat[row_index, column_index]
在哪里
- 数据框是输入数据框
- row_index 是行号
- column_index 是列号
示例:获取数据集第一行的程序
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using iat() function of
# first row 2 nd column
print(data.iat[0, 1])
# get first row using iat() function of
# first row 1 st column
print(data.iat[0, 0])
# get first row using iat() function of
# first row 3 rd column
print(data.iat[0, 2])
输出:
sravan
7058
java
方法 6:使用 at[]函数
此函数采用列名和第一行索引来显示数据帧的第一行。
语法:
dataframe.at[row_index, column_name]
在哪里
- 数据框是输入数据框
- row_index – 0 获取第一行
- column_name 是列的名称
示例:获取数据集第一行的程序
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# get first row using iat() function of
# first row 2 nd column
print(data.at[0, 'name'])
# get first row using iat() function of
# first row 1 st column
print(data.at[0, 'id'])
# get first row using iat() function of
# first row 3 rd column
print(data.at[0, 'subjects'])
输出:
sravan
7058
java