如何在Jupyter Notebook中打开CSV文件?

时间:2019-07-06 06:30:47

标签: python pandas csv jupyter-notebook

我想使用Pandas将本地计算机中的CSV文件包含到Jupyter Notebook中,但是每次出现文件未找到错误。

我尝试包含使用不同目录的文件以及在文件路径之前使用r的文件。

import numpy as np 
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
df=pd.read_csv(r'C:\Users\shrey\Desktop\MINDTREE.csv')

The error message I am getting is the following:
    --------------------------------------------------------------------------
    FileNotFoundError                         Traceback (most recent call last)
    <ipython-input-23-d0297c50883f> in <module>()
    ----> 1 df=pd.read_csv(r'C:\Users\shrey\Desktop\MINDTREE.csv')

    /opt/anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, doublequote, delim_whitespace, low_memory, memory_map, float_precision)
        676                     skip_blank_lines=skip_blank_lines)
        677 
    --> 678         return _read(filepath_or_buffer, kwds)
        679 
        680     parser_f.__name__ = name

    /opt/anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
        438 
        439     # Create the parser.
    --> 440     parser = TextFileReader(filepath_or_buffer, **kwds)
        441 
        442     if chunksize or iterator:

    /opt/anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
        785             self.options['has_index_names'] = kwds['has_index_names']
        786 
    --> 787         self._make_engine(self.engine)
        788 
        789     def close(self):

    /opt/anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in _make_engine(self, engine)
       1012     def _make_engine(self, engine='c'):
       1013         if engine == 'c':
    -> 1014             self._engine = CParserWrapper(self.f, **self.options)
       1015         else:
       1016             if engine == 'python':

    /opt/anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
       1706         kwds['usecols'] = self.usecols
       1707 
    -> 1708         self._reader = parsers.TextReader(src, **kwds)
       1709 
       1710         passed_names = self.names is None

    pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

    pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

    FileNotFoundError: File b'C:\\Users\\shrey\\Desktop\\MINDTREE.csv' does not exist

2 个答案:

答案 0 :(得分:0)

'\'是python中的转义字符,Windows使用它们来分隔路径。

尝试使用:

df=pd.read_csv('C:\\Users\\shrey\\Desktop\\MINDTREE.csv')

df=pd.read_csv('C:/Users/shrey/Desktop/MINDTREE.csv')

答案 1 :(得分:0)

您可以将csv放入保存jupyter笔记本的目录中,也可以只使用熊猫read_csv

df=pd.read_csv('C:\\Users\\shrey\\Desktop\\MINDTREE.csv')