【问题标题】:Constantly Getting Slice Indexing Error With Pandas excel_read in Jupyter在 Jupyter 中使用 Pandas excel_read 不断出现切片索引错误
【发布时间】:2019-08-02 07:56:29
【问题描述】:

这是我试图在 Jupyter Notebook 中运行的 疯狂 简单脚本。我对此完全陌生,但似乎能够访问正确的目录并查看正确的文件。 help.xlsx 只是一个简单的excel文件,第一行是几个字符串,下面是相应的数字行。它的意思是作为一个测试文件,因为实际上其他任何东西都不起作用。我已经尝试使用 csv 文件和在线提供的文件来读取 csv 文件。他们可以阅读它,但我一直收到同样的错误(我会放在下面)

import pandas as pd
import seaborn as sns

def io_hw(out_path):
    df = pd.read_excel('./help.xlsx')
    head_df = df.head(df)
    head_df.to_csv('./tester.csv')
    return df, head_df

错误:

TypeError: cannot do slice indexing on <class 'pandas.core.indexes.range.RangeIndex'> with these indexers [   hello  this   is  awful
E       0      1     4    7      6
E       1      2     3    5      8
E       2     12    11    9     10] of <class 'pandas.core.frame.DataFrame'>

/Users/Shea/miniconda3/envs/em212/lib/python3.7/site-packages/pandas/core/indexes/base.py:3066: TypeError

无论我做什么,无论我使用什么文件,每次都是 exact 相同的错误(只是使用不同的无效索引),我尝试过谷歌搜索,在这里查找,没有似乎可以解决问题。非常感谢任何帮助。

【问题讨论】:

    标签: python excel pandas indexing jupyter-notebook


    【解决方案1】:

    错误来自这个语句:df.head(df)

    在 pandas 的 .head() 模块中,您必须输入行数,而不是两次指定数据框。例如,如果你想要拳头 3 行,你可以说df.head(n=3)

    如果你想让所有的行不用.head()模块,直接将excel DF保存到CSV,像这样:df.to_csv('./tester.csv')

    这个的官方文档是here

    【讨论】:

      猜你喜欢
      • 2013-01-21
      • 1970-01-01
      • 2018-12-11
      • 2021-12-31
      • 2021-07-18
      • 1970-01-01
      • 2013-08-03
      • 2018-11-22
      • 2016-10-09
      相关资源
      最近更新 更多