Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Pandas DataFrame rename() 方法

❮ DataFrame 参考


示例

重命名 DataFrame 的行索引

import pandas as pd

data = {
"age": [50, 40, 30],
"qualified": [True, False, False]
}
idx = ["Sally", "Mary", "John"]
df = pd.DataFrame(data, index=idx)

newdf = df.rename({"Sally": "Pete", "Mary": "Patrick", "John": "Paula"})

print(newdf)
自己试试 »

定义和用法

The rename() 方法允许您更改行索引和列标签。


语法

dataframe.rename(mapper, index, columns, axis, copy, inplace, level, errors)

参数

The index, columns, axis, copy, inplace, level , errors 参数是 关键字参数

参数 描述
mapper   可选。一个字典,其中旧索引/标签是键,新索引/标签是值
index 旧索引和新索引作为键/值对 可选。一个字典,其中旧索引是键,新索引是值
columns 旧标签和新标签作为键/值对 可选。一个字典,其中旧标签是键,新标签是值
axis 0
1
'index'
'columns'
可选,默认为 0。执行重命名的轴(如果 mapper 参数存在且 index 或 columns 不存在,则很重要)
copy True
False
可选,默认为 True。是否也复制底层数据
inplace True
False
可选,默认为 False。如果为 True:则在当前 DataFrame 上执行操作。如果为 False:则返回执行操作的副本。
level 数字
标签
可选,在处理 MultiIndex DataFrames 时指定要重命名的级别
errors 'ignore'
'raise'
可选,默认为 'ignore'。指定如果 DataFrame 中不存在此类索引/标签是否返回错误

返回值

具有结果的 DataFrame,如果 inplace 参数设置为 True,则返回 None。

此函数不会对原始 DataFrame 对象进行更改。


❮ DataFrame 参考

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.