获取dataframe的第一行

简介: 在pandas中,可以使用`iloc`函数获取dataframe的第一行。以下是一个例子

在pandas中,可以使用iloc函数获取dataframe的第一行。以下是一个例子:

import pandas as pd

# 创建一个dataframe
df = pd.DataFrame({
   
    'A': ['A0', 'A1', 'A2', 'A3'],
    'B': ['B0', 'B1', 'B2', 'B3'],
    'C': ['C0', 'C1', 'C2', 'C3']
})

# 获取第一行
df_first_row = df.iloc[0]

print(df_first_row)

在这个例子中,我们首先创建了一个dataframe,然后使用iloc函数获取第一行。iloc函数的第一个参数表示行索引,第二个参数表示列索引。因为我们只关心行索引,所以第一个参数是0。
输出结果如下:

A    A0
B    B0
C    C0
Name: 0, dtype: object

可以看到,我们成功地获取了dataframe的第一行,并将它保存为一个新的dataframe。

相关文章
dataframe获取指定列
dataframe获取指定列
1547 0
|
Python
dataframe添加一新列
dataframe添加一新列
2786 2
|
Python
在dataframe中插入新的一行
在pandas中,可以使用`insert`函数在dataframe中插入新的一行。
1514 1
|
Python
df获取最后一行数据
df获取最后一行数据
1287 0
DataFrame(12):数据转换——apply(),applymap()函数的使用(一)
DataFrame(12):数据转换——apply(),applymap()函数的使用(一)
DataFrame(12):数据转换——apply(),applymap()函数的使用(一)
|
Python
Python中如何按行遍历DataFrame
听世界的意见,保留自己的态度。
2978 0
|
SQL 索引 Python
Pandas中DataFrame合并的几种方法
Pandas中DataFrame合并的几种方法
2411 1
|
数据挖掘 索引 Python
python_DataFrame的loc和iloc取数据 基本方法总结
python_DataFrame的loc和iloc取数据 基本方法总结
2596 0
python_DataFrame的loc和iloc取数据 基本方法总结
|
索引 Python
Python 教程之 Pandas(7)—— 遍历 Pandas DataFrame 中的行和列
Python 教程之 Pandas(7)—— 遍历 Pandas DataFrame 中的行和列
1595 0
|
数据采集 机器学习/深度学习 数据处理
DataFrame 操作
DataFrame 操作
1042 1

热门文章

最新文章