pandas dataframe 删除索引-ag捕鱼王app官网

pandas dataframe 删除索引

作者:迹忆客 最近更新:2024/04/24 浏览次数:

本教程将介绍如何删除 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 read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:python

pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。

pandas 追加数据到 csv 中

发布时间:2024/04/24 浏览次数:352 分类:python

本教程演示了如何在追加模式下使用 to_csv()向现有的 csv 文件添加数据。

pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:python

本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。

pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:python

本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图