Python 删除行索引数据框熊猫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43716402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:19:14  来源:igfitidea点击:

Remove Row Index dataframe pandas

pythonpandas

提问by bryanblackbee

Given a df

给定一个 df

in[0]df1
out[0]
        DATE    REVENUE    COST    POSITION
FACTOR
0    2017/01/01    1000    900    10
1    2017/01/01    900     700    9
2    2017/01/01    1100    800    7

I have an additional row FACTOR. After trying reset_index()and other ways, I cannot remove the FACTORmulti (row) index. Is there a way to do so?

我有一个额外的行FACTOR。经过尝试reset_index()和其他方式,我无法删除FACTOR多(行)索引。有没有办法这样做?

I know it's common to drop columns and reset index but not this way though.

我知道删除列和重置索引很常见,但不是这样。

回答by Mr.Pacman

I hope this works :)

我希望这有效:)

df.reset_index(inplace=True) # Resets the index, makes factor a column
df.drop("Factor",axis=1,inplace=True) # drop factor from axis 1 and make changes permanent by inplace=True

回答by zipa

Try using:

尝试使用:

df1.reset_index(drop=True)

This resets the index to the default integer index and removes the original one. If you want to assign this change to original dataframeit is easier to use:

这会将索引重置为默认整数索引并删除原始索引。如果您想将此更改分配给原件dataframe,则使用起来更容易:

df1.reset_index(drop=True, inplace=True)

As it will edit the df1dataframe without making a copy of it.

因为它将编辑df1数据框而不复制它。

回答by MaxU

FACTORis the name of the index - you shouldn't worry about it - it doesn't affect your data:

FACTOR是索引的名称 - 您不必担心 - 它不会影响您的数据:

In [78]: df
Out[78]:
              DATE  REVENUE  COST  POSITION
FACTOR
10      2017/01/01     1000   900        10
11      2017/01/01      900   700         9
12      2017/01/01     1100   800         7

In [79]: df.index.name
Out[79]: 'FACTOR'

If you want to rename it or to get rid of it (preserving the index values) you can use DataFrame.rename_axis()method:

如果要重命名或删除它(保留索引值),可以使用DataFrame.rename_axis()方法:

In [80]: df = df.rename_axis(None)

In [81]: df
Out[81]:
          DATE  REVENUE  COST  POSITION
10  2017/01/01     1000   900        10
11  2017/01/01      900   700         9
12  2017/01/01     1100   800         7

In [82]: df.index.name is None
Out[82]: True