【问题标题】:Removing Row Indices and Reverting them to a Column删除行索引并将它们恢复为列
【发布时间】:2020-10-22 01:27:51
【问题描述】:

我有一个如下所示的 pandas 数据框。

我想创建一个条形图;但是,我不能,因为LotType 不被视为列变量名称,而是行标题。我怎样才能像其他专栏一样制作LotType?我确信这是一个简单的解决方法,但我一直无法找到答案。

bigdf.plot.bar(x='LotType', y='Ratio2015', figsize=(20, 3), color='maroon')

作为参考,这是我在上面解释过的错误:

KeyError                                  Traceback (most recent call last)
...
KeyError: 'LotType'

【问题讨论】:

标签: python python-3.x pandas dataframe keyerror


【解决方案1】:

看看reset_index方法:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reset_index.html

df.reset_index() 将创建一个仅包含数字的新索引列,并且您的 LotType 列将保存为普通列。

【讨论】:

    猜你喜欢
    • 2014-03-07
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 2020-08-04
    • 1970-01-01
    • 2020-08-30
    • 2015-06-24
    相关资源
    最近更新 更多