熊猫上的read_excel或read_csv

时间:2020-09-12 19:24:45

标签: python excel pandas

我尝试了pandas方法,但是它不起作用。我的文件名为“ Classeur1”。我无法在Jupyter或Spyder上加载它。

import pandas as pd

df = pd.read_csv("TP 2 FIN 3500.xlsx")

df.head()

我得到:

  File "<ipython-input-1-2b6f31d9e40c>", line 1, in <module>
    runfile('C:/Users/alber/.spyder-py3/temp.py', wdir='C:/Users/alber/.spyder-py3')

  File "C:\Users\alber\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\alber\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/alber/.spyder-py3/temp.py", line 2, in <module>
    df = pd.read_csv("TP 2 FIN 3500.xlsx")

  File "C:\Users\alber\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 685, in parser_f
    return _read(filepath_or_buffer, kwds)

  File "C:\Users\alber\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 457, in _read
    parser = TextFileReader(fp_or_buf, **kwds)

  File "C:\Users\alber\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
    self._make_engine(self.engine)

  File "C:\Users\alber\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1135, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)

  File "C:\Users\alber\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1917, in __init__
    self._reader = parsers.TextReader(src, **kwds)

  File "pandas\_libs\parsers.pyx", line 382, in pandas._libs.parsers.TextReader.__cinit__

  File "pandas\_libs\parsers.pyx", line 689, in pandas._libs.parsers.TextReader._setup_parser_source

FileNotFoundError: [Errno 2] File b'TP 2 FIN 3500.xlsx' does not exist: b'TP 2 FIN 3500.xlsx'

I also tried both method with the absolute: C:\Users\alber\Documents\Downloads\python stockage

无法使用pandas数据框加载文件

谢谢!

3 个答案:

答案 0 :(得分:0)

要阅读excel,请尝试此。

df = pd.read_excel("TP 2 FIN 3500.xlsx")

如果Classeur1是CSV文件,请尝试此操作。

df = pd.read_excel("Classeur1.csv")

还要检查该文件是否与笔记本计算机放在同一目录中。

答案 1 :(得分:0)

您按以下方式阅读excel文件:

df = pd.read_excel (r'Path where the Excel file is stored\File name.xlsx')

答案 2 :(得分:0)

如果您使用的是pd.read_csv(),则将excel文件转换为.CSV formate。 Aslo将您的excel或.csv文件放在以下目录中:C:\ Users \ alber。

df= pd.read_csv("your_filename.csv")

您还可以使用以下代码通过更改jupyter运行目录路径来使用另一种方法:-

os.chdir('D:\folder_path_of_csv_file')
print(os.getcwd())
df= pd.read_csv("your_filename.csv")