gpt4 book ai didi

python - Pandas 从 Int64Index 转换为 RangeIndex

转载 作者:行者123 更新时间:2023-11-30 21:59:24 26 4
gpt4 key购买 nike

我连接了三个数据帧。如何在 RangeIndex 中打印 df.index,而不是 Int64Index?

我的输入:

df = pd.concat([df1, df2, df3])
print(df.index)

我的输出:

Int64Index([    0,     1,     2,     3,     4,     5,     6,     7,     8,
9,
...
73809, 73810, 73811, 73812, 73813, 73814, 73815, 73816, 73817,
73818],
dtype='int64', length=495673)

所需输出:

RangeIndex(start=X, stop=X, step=X)

最佳答案

您可以使用reset_index来获取所需的索引。例如:

df = pd.concat([df1,df2,df3])
df.index

Int64Index([0, 1, 2, 0, 1, 2, 0, 1, 2], dtype='int64')

重置索引后:

df.reset_index(inplace=True)
df.index

RangeIndex(start=0, stop=9, step=1)

此外,在 concat 函数中使用 axis 关键字也很好。

关于python - Pandas 从 Int64Index 转换为 RangeIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54603378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com