重新索引熊猫数据框-python

时间:2019-02-18 19:21:46

标签: python python-3.x pandas dataframe

我有一个数据框,但掉了一部分。现在,如果我们将数据框视为表,则我的新数据框没有所有行。 enter image description here

我想改变

1

2

3

11 。 。

0

1

2

3

4 。 。

谢谢。

1 个答案:

答案 0 :(得分:3)

使用reset_index()和可选参数drop=True

import pandas as pd

df = pd.DataFrame({
        'A0' : list(range(10)),
        'A1' : list(range(10)),
        'A2' : list(range(10)),
        '3A' : list(range(10)),
        'A4' : list(range(10)),
        'A5' : list(range(10))
    })
print(df.head())
#    A0  A1  A2  3A  A4  A5
# 0   0   0   0   0   0   0
# 1   1   1   1   1   1   1
# 2   2   2   2   2   2   2
# 3   3   3   3   3   3   3
# 4   4   4   4   4   4   4

df = df.iloc[2:4]
print(df)
#    A0  A1  A2  3A  A4  A5
# 2   2   2   2   2   2   2
# 3   3   3   3   3   3   3

df = df.reset_index(drop=True)
print(df)
#    A0  A1  A2  3A  A4  A5
# 0   2   2   2   2   2   2
# 1   3   3   3   3   3   3
相关问题