pandas dataframe 删除索引
本教程将介绍如何删除 pandas dataframe 的索引。
我们将使用下面显示的 dataframe 来展示如何删除索引。
import pandas as pd
my_df = pd.dataframe(
{
"person": ["alice", "steven", "neesham", "chris", "alice"],
"city": ["berlin", "montreal", "toronto", "rome", "munich"],
"mother tongue": ["german", "french", "english", "italian", "german"],
"age": [37, 20, 38, 23, 35],
},
index=["a", "b", "c", "d", "e"],
)
print(my_df)
输出:
person city mother tongue age
a alice berlin german 37
b steven montreal french 20
c neesham toronto english 38
d chris rome italian 23
e alice munich german 35
使用 reset_index()
方法删除 pandas dataframe 的索引
pandas.dataframe.reset_index()
会将 dataframe 的索引重置为默认索引。
import pandas as pd
my_df = pd.dataframe(
{
"person": ["alice", "steven", "neesham", "chris", "alice"],
"city": ["berlin", "montreal", "toronto", "rome", "munich"],
"mother tongue": ["german", "french", "english", "italian", "german"],
"age": [37, 20, 38, 23, 35],
},
index=["a", "b", "c", "d", "e"],
)
df_reset = my_df.reset_index()
print("before reseting index:")
print(my_df, "\n")
print("after reseting index:")
print(df_reset)
输出:
before reseting index:
person city mother tongue age
a alice berlin german 37
b steven montreal french 20
c neesham toronto english 38
d chris rome italian 23
e alice munich german 35
after reseting index:
index person city mother tongue age
0 a alice berlin german 37
1 b steven montreal french 20
2 c neesham toronto english 38
3 d chris rome italian 23
4 e alice munich german 35
它将重置 dataframe 的索引,但现在的索引将显示为 index
列。如果我们想删除 index
列,我们可以在 reset_index()
方法中设置 drop=true
。
import pandas as pd
my_df = pd.dataframe(
{
"person": ["alice", "steven", "neesham", "chris", "alice"],
"city": ["berlin", "montreal", "toronto", "rome", "munich"],
"mother tongue": ["german", "french", "english", "italian", "german"],
"age": [37, 20, 38, 23, 35],
},
index=["a", "b", "c", "d", "e"],
)
df_reset = my_df.reset_index(drop=true)
print("before reseting index:")
print(my_df, "\n")
print("after reseting index:")
print(df_reset)
输出:
before reseting index:
person city mother tongue age
a alice berlin german 37
b steven montreal french 20
c neesham toronto english 38
d chris rome italian 23
e alice munich german 35
after reseting index:
person city mother tongue age
0 alice berlin german 37
1 steven montreal french 20
2 neesham toronto english 38
3 chris rome italian 23
4 alice munich german 35
使用 set_index()
方法删除 pandas dataframe 的索引
pandas.dataframe.set_index()
将把作为参数传递的列设置为 dataframe 的索引,覆盖初始索引。
import pandas as pd
my_df = pd.dataframe(
{
"person": ["alice", "steven", "neesham", "chris", "alice"],
"city": ["berlin", "montreal", "toronto", "rome", "munich"],
"mother tongue": ["german", "french", "english", "italian", "german"],
"age": [37, 20, 38, 23, 35],
},
index=["a", "b", "c", "d", "e"],
)
df_reset = my_df.set_index("person")
print("initial dataframe:")
print(my_df, "\n")
print("after setting person column as index:")
print(df_reset)
输出:
initial dataframe:
person city mother tongue age
a alice berlin german 37
b steven montreal french 20
c neesham toronto english 38
d chris rome italian 23
e alice munich german 35
after setting person column as index:
city mother tongue age
person
alice berlin german 37
steven montreal french 20
neesham toronto english 38
chris rome italian 23
alice munich german 35
它将 person
列设置为 my_df
dataframe 的索引,覆盖了 dataframe 的初始索引。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串