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 reindex() 方法

❮ DataFrame 参考


示例

更改 DataFrame 的行索引

import pandas as pd

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

newidx = ["Robert", "Cindy", "Chloe", "Pete"]
newdf = df.reindex(newidx)
print(newdf)
自己试试 »

定义和用法

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

;]

注意:如果新索引与旧索引不同,则值将设置为 NaN。


语法

dataframe.reindex(keys, method, copy, level, fill_value, limit, tolerance)

参数

methodcopylevelfill_valuelimittolerance 参数是 关键字参数

参数 描述
keys   必需。包含行索引或列标签的字符串或列表
method None
'backfill'
'bfill'
'pad'
'ffill'
'nearest'
可选,默认为 None。指定在填充索引中的空洞时使用的方法。仅适用于递增/递减索引。
copy True
False
可选,默认为 True。当所有新索引与旧索引相同时,是否返回一个新对象(副本)
level 数字
标签
可选
fill_value 值列表 可选,默认为 NaN。指定用于缺失值的值
limit 数字 可选,默认为 None。
tolerance   可选

返回值

包含结果的 DataFrame

此函数不会对原始 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.