迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

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 浏览次数:181 分类:Python

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

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:70 分类:Python

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

Pandas 多列合并

发布时间:2024/04/24 浏览次数:190 分类:Python

本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:140 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便