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

❮ DataFrame 参考


示例

使“name”列成为 DataFrame 的索引

import pandas as pd

data = {
  "name": ["Sally", "Mary", "John", "Monica"],
  "age": [50, 40, 30, 40],
  "qualified": [True, False, False, False]
}

df = pd.DataFrame(data)

newdf = df.set_index('name')
自己试试 »

定义和用法

The set_index() 方法允许一个或多个列值成为行索引。


语法

dataframe.set_index(keys, drop, append, inplace, verify_integrity)

参数

dropappendinplace verify_integrity 参数是关键字参数

参数 描述
keys   必填。包含列标签和/或列键的字符串或列表
drop True
False
可选,默认为 True。设置为 False 时,设置为行索引的列仍应保留为列
append True
False
可选,默认为 False。如果新的行索引应附加到现有索引(默认情况下,现有索引会被覆盖),则设置为 True
inplace True
False
可选,默认为 False。如果为 True:则在当前 DataFrame 上执行操作。如果为 False:则返回执行了操作的副本。
verify_integrity True
False
可选,默认为 False。指定是否应检查新索引是否存在重复项

返回值

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


❮ 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.