pandas dataframe 重置索引-ag捕鱼王app官网

pandas dataframe 重置索引

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

本教程介绍了如何使用 pandas.dataframe.reset_index() 来重置 pandas dataframe 中的索引。reset_index() 方法将 dataframe 的索引设置为默认索引,数字范围从 0(dataframe 中的行数-1)


pandas dataframe reset_index() 方法

语法

dataframe.reset_index(level=none, drop=false, inplace=false, col_level=0, col_fill="")

使用 pandas.dataframe.reset_index() 方法重置 dataframe 的索引

import pandas as pd
roll_no = [501, 502, 503, 504, 505]
student_df = pd.dataframe(
    {
        "name": ["alice", "steven", "neesham", "chris", "alice"],
        "age": [17, 20, 18, 21, 15],
        "city": ["new york", "portland", "boston", "seattle", "austin"],
        "grade": ["a", "b-", "b ", "a-", "a"],
    },
    index=roll_no,
)
print(student_df)

输出:

        name  age      city grade
501    alice   17  new york     a
502   steven   20  portland    b-
503  neesham   18    boston    b 
504    chris   21   seattle    a-
505    alice   15    austin     a

假设我们有一个 dataframe,有 5 行 4 列,如输出所示。我们在 dataframe 中还设置了一个索引。

重置 dataframe 的索引,保持 dataframe 的初始索引为列

import pandas as pd
roll_no = [501, 502, 503, 504, 505]
student_df = pd.dataframe(
    {
        "name": ["alice", "steven", "neesham", "chris", "alice"],
        "age": [17, 20, 18, 21, 15],
        "city": ["new york", "portland", "boston", "seattle", "austin"],
        "grade": ["a", "b-", "b ", "a-", "a"],
    },
    index=roll_no,
)
print("initial dataframe:")
print(student_df)
print("")
print("dataframe after reset_index:")
student_df.reset_index(inplace=true, drop=false)
print(student_df)

输出:

initial dataframe:
        name  age      city grade
501    alice   17  new york     a
502   steven   20  portland    b-
503  neesham   18    boston    b 
504    chris   21   seattle    a-
505    alice   15    austin     a
dataframe after reset_index:
   index     name  age      city grade
0    501    alice   17  new york     a
1    502   steven   20  portland    b-
2    503  neesham   18    boston    b 
3    504    chris   21   seattle    a-
4    505    alice   15    austin     a

它将 dataframe student_df 的索引重置为默认索引。inplace=true 会在原 dataframe 本身进行更改,如果我们使用 drop=false,初始索引会被放置在 dataframe 中作为列。如果我们使用 drop=false,在使用 reset_index() 方法后,初始索引会被放置在 dataframe 中作为一列。

重置 dataframe 的索引,删除 dataframe 的初始索引

import pandas as pd
roll_no = [501, 502, 503, 504, 505]
student_df = pd.dataframe(
    {
        "name": ["alice", "steven", "neesham", "chris", "alice"],
        "age": [17, 20, 18, 21, 15],
        "city": ["new york", "portland", "boston", "seattle", "austin"],
        "grade": ["a", "b-", "b ", "a-", "a"],
    },
    index=roll_no,
)
print("initial dataframe:")
print(student_df)
print("")
print("dataframe after reset_index:")
student_df.reset_index(inplace=true, drop=true)
print(student_df)

输出:

initial dataframe:
        name  age      city grade
501    alice   17  new york     a
502   steven   20  portland    b-
503  neesham   18    boston    b 
504    chris   21   seattle    a-
505    alice   15    austin     a
dataframe after reset_index:
      name  age      city grade
0    alice   17  new york     a
1   steven   20  portland    b-
2  neesham   18    boston    b 
3    chris   21   seattle    a-
4    alice   15    austin     a

它将 dataframe student_df 的索引重置为默认索引。由于我们在 reset_index() 方法中设置了 drop=true,初始索引从 dataframe 中被删除。

删除行后重置 dataframe 的索引

import pandas as pd
roll_no = [501, 502, 503, 504, 505]
student_df = pd.dataframe(
    {
        "name": ["alice", "steven", "neesham", "chris", "alice"],
        "age": [17, 20, 18, 21, 15],
        "city": ["new york", "portland", "boston", "seattle", "austin"],
        "grade": ["a", "b-", "b ", "a-", "a"],
    }
)
student_df.drop([2, 3], inplace=true)
print("initial dataframe:")
print(student_df)
print("")
student_df.reset_index(inplace=true, drop=true)
print("dataframe after reset_index:")
print(student_df)

输出:

initial dataframe:
     name  age      city grade
0   alice   17  new york     a
1  steven   20  portland    b-
4   alice   15    austin     a
dataframe after reset_index:
     name  age      city grade
0   alice   17  new york     a
1  steven   20  portland    b-
2   alice   15    austin     a

正如我们在输出中所看到的,我们在删除行后有缺失的索引。在这种情况下,我们可以使用 reset_index() 方法来使用没有缺失值的索引。

如果我们希望将初始索引作为 dataframe 的列,我们可以在 reset_index() 方法中使用 drop=false

转载请发邮件至 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

最新推荐

教程更新

热门标签

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