在 Pandas 中删除 DataFrame 的前三行
pythonserver side programmingprogramming更新于 2025/10/27 21:00:17
要在 Pandas 中删除 DataFrame 的前三行,我们可以使用 iloc() 方法。
步骤
- 创建一个二维、大小可变、可能异构的表格数据 df。
- 打印输入 DataFrame df。
- 使用 df.iloc[3:] 删除前三行。
- 打印更新的 DataFrame。
示例
import pandas as pd
df = pd.DataFrame(
{
"x": [5, 2, 7, 0, 7, 0, 5, 2],
"y": [4, 7, 5, 1, 5, 1, 4, 7],
"z": [9, 3, 5, 1, 5, 1, 9, 3]
}
)
print "Input DataFrame is:\n", df
df = df.iloc[3:]
print "After deleting the first 3 rows: \n", df
输出
Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 4 7 5 5 5 0 1 1 6 5 4 9 7 2 7 3 After deleting the first 3 rows: x y z 3 0 1 1 4 7 5 5 5 0 1 1 6 5 4 9 7 2 7 3
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

