Pandas DataFrame truncate() 方法
实例
删除 DataFrame 位置 3 之前和位置 5 之后的行:
import pandas as pddata = {"age": [50, 40, 30, 40, 20, 10, 30],"qualified": [True, False, False, False, False, True, True]}df = pd.DataFrame(data)newdf = df.truncate(before=3, after=5)print(newdf)
定义与用法
truncate() 方法删除指定索引或标签前后的元素。
使用 axis='columns' 参数移除指定列。
语法
dataframe.truncate(before, after, axis, copy)
参数
index, columns,axis, copy, inplace 参数都是 关键字参数。
| 参数 | 值 | 描述 |
|---|---|---|
| before | Number Label Date | 可选。 删除此值之前的所有内容 |
| after | Number Label Date | 可选。 删除此值之后的所有内容 |
| axis | 0 1 'index' 'columns' | 可选, 默认值 0。需要删除的列 |
| copy | True False | 可选, 默认值 True。指定是否返回 DataFrame 的副本 |
返回值
一个 DataFrame 结果。