如何将“脏” csv文件导入大熊猫并绘图?

时间:2020-09-21 20:11:22

标签: python python-3.x pandas csv matplotlib

这是我尝试的代码。 .csv文件有一个包含日期(dd-mm-yyyy)的列。我想创建一个像this这样的图形。这是.csv文件,仅包含2列!

    from pandas import read_csv
    from matplotlib import pyplot
    series = read_csv(r'C:\Users\HP\Desktop\archive\daily-minimum-temperatures-in-me.csv', header=0, index_col=0)
    series.plot()
    pyplot.show

我得到的错误是: 没有要绘制的数字数据 这一定是因为.csv file仅具有2列(日期,温度),并且由于无法读取日期,因此它仅考虑1列并给我该错误。

请帮助我显示此graph的日期 我从this website获得了.csv文件和图形,请在需要时引用它并帮我解决

错误

---------------------------------------------------------------------------
ParserError                               Traceback (most recent call last)
<ipython-input-223-040e03668a43> in <module>
----> 1 pd.read_csv('data/archive/daily-minimum-temperatures-in-me.csv', header=0, index_col=0)

e:\Anaconda3\lib\site-packages\pandas\io\parsers.py in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
    684     )
    685 
--> 686     return _read(filepath_or_buffer, kwds)
    687 
    688 

e:\Anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
    456 
    457     try:
--> 458         data = parser.read(nrows)
    459     finally:
    460         parser.close()

e:\Anaconda3\lib\site-packages\pandas\io\parsers.py in read(self, nrows)
   1184     def read(self, nrows=None):
   1185         nrows = _validate_integer("nrows", nrows)
-> 1186         ret = self._engine.read(nrows)
   1187 
   1188         # May alter columns / col_dict

e:\Anaconda3\lib\site-packages\pandas\io\parsers.py in read(self, nrows)
   2143     def read(self, nrows=None):
   2144         try:
-> 2145             data = self._reader.read(nrows)
   2146         except StopIteration:
   2147             if self._first_chunk:

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader.read()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._read_low_memory()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._read_rows()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._tokenize_rows()

pandas\_libs\parsers.pyx in pandas._libs.parsers.raise_parser_error()

ParserError: Error tokenizing data. C error: Expected 2 fields in line 3653, saw 3

数据

  • 文件的顶部和底部
"Date","Daily minimum temperatures in Melbourne, Australia, 1981-1990"
"1981-01-01",20.7
"1981-01-02",17.9
"1981-01-03",18.8
"1981-01-04",14.6
"1981-01-05",15.8
"1981-01-06",15.8
"1981-01-07",15.8
"1981-01-08",17.4
"1981-01-09",21.8
"1981-01-10",20.0
"1981-01-11",16.2
"1981-01-12",13.3
"1981-01-13",16.7
"1981-01-14",21.5
"1981-01-15",25.0
"1981-01-16",20.7
"1981-01-17",20.6
"1981-01-18",24.8
"1981-01-19",17.7
"1981-01-20",15.5
"1981-01-21",18.2
"1981-01-22",12.1
"1981-01-23",14.4
"1981-01-24",16.0
"1981-01-25",16.5
"1981-01-26",18.7
"1981-01-27",19.4
"1981-01-28",17.2
"1981-01-29",15.5
"1981-01-30",15.1
"1982-07-10",2.8
"1982-07-11",4.0
"1982-07-12",7.5
"1982-07-13",7.8
"1982-07-14",5.6
"1982-07-15",3.3
"1982-07-16",5.0
"1982-07-17",3.7
"1982-07-18",3.9
"1982-07-19",5.2
"1982-07-20",?0.2
"1982-07-21",?0.8
"1982-07-22",0.9
"1982-07-23",3.5
"1982-07-24",6.6
"1982-07-25",9.5
"1982-07-26",9.0
"1982-07-27",3.5
"1982-07-28",4.5
"1982-07-29",5.7
"1982-07-30",5.6
"1982-07-31",7.1
"1982-08-01",9.7
"1982-08-02",8.3
"1982-08-03",9.1
"1981-01-31",15.4
"1981-02-01",15.3
"1981-02-02",18.8
"1981-02-03",21.9
"1981-02-04",19.9
"1981-02-05",16.6
"1981-02-06",16.8
"1981-02-07",14.6
"1981-02-08",17.1
"1981-02-09",25.0
"1981-02-10",15.0
"1981-02-11",13.7
"1981-02-12",13.9
"1981-02-13",18.3
"1981-02-14",22.0
"1981-02-15",22.1
"1981-02-16",21.2
"1981-02-17",18.4
"1981-02-18",16.6
"1981-02-19",16.1
"1990-09-18",7.3
"1990-09-19",11.6
"1990-09-20",16.3
"1990-09-21",9.6
"1990-09-22",6.8
"1990-09-23",5.2
"1990-09-24",10.6
"1990-09-25",16.3
"1990-09-26",9.8
"1990-09-27",4.6
"1990-09-28",11.1
"1990-09-29",8.7
"1990-09-30",10.0
"1990-10-01",11.3
"1990-10-02",10.5
"1990-10-03",9.9
"1990-10-04",11.0
"1990-10-05",14.0
"1990-10-06",9.2
"1990-10-07",9.8
"1990-10-08",6.0
"1990-10-09",9.8
"1990-10-10",9.2
"1990-10-11",11.8
"1990-10-12",10.3
"1990-10-13",7.5
"1990-10-14",7.7
"1990-10-15",15.8
"1990-10-16",14.6
"1990-10-17",10.5
"1990-10-18",11.3
"1990-10-19",10.9
"1990-10-20",6.4
"1990-10-21",10.9
"1990-10-22",9.0
"1990-10-23",10.9
"1990-10-24",12.4
"1990-10-25",11.6
"1990-10-26",13.3
"1990-10-27",14.4
"1990-10-28",18.4
"1990-10-29",13.6
"1990-10-30",14.9
"1990-10-31",14.8
"1990-11-01",15.4
"1990-11-02",11.8
"1990-11-03",13.0
"1990-11-04",11.1
"1990-11-05",12.5
"1990-11-06",18.3
"1990-11-07",19.2
"1990-11-08",15.4
"1990-11-09",13.1
"1990-11-10",11.5
"1990-11-11",8.6
"1990-11-12",12.6
"1990-11-13",13.8
"1990-11-14",14.6
"1990-11-15",13.2
"1990-11-16",12.3
"1990-11-17",8.8
"1990-11-18",10.7
"1990-11-19",9.9
"1990-11-20",8.3
"1990-11-21",15.0
"1990-11-22",12.2
"1990-11-23",10.5
"1990-11-24",11.1
"1990-11-25",13.0
"1990-11-26",12.9
"1990-11-27",8.8
"1990-11-28",14.7
"1990-11-29",14.7
"1990-11-30",12.7
"1990-12-01",13.3
"1990-12-02",13.2
"1990-12-03",16.2
"1990-12-04",17.3
"1990-12-05",20.5
"1990-12-06",20.2
"1990-12-07",19.4
"1990-12-08",15.5
"1990-12-09",14.1
"1990-12-10",11.0
"1990-12-11",11.1
"1990-12-12",14.0
"1990-12-13",11.4
"1990-12-14",12.5
"1990-12-15",13.4
"1990-12-16",13.6
"1990-12-17",13.9
"1990-12-18",17.2
"1990-12-19",14.7
"1990-12-20",15.4
"1990-12-21",13.1
"1990-12-22",13.2
"1990-12-23",13.9
"1990-12-24",10.0
"1990-12-25",12.9
"1990-12-26",14.6
"1990-12-27",14.0
"1990-12-28",13.6
"1990-12-29",13.5
"1990-12-30",15.7
"1990-12-31",13.0

Daily minimum temperatures in Melbourne, Australia, 1981-1990


1 个答案:

答案 0 :(得分:2)

  • 数据存在一些需要清理的问题
  • 文件末尾的额外文本行,可以用git branch跳过
  • 尝试在skiprows中绘制数据结果,这意味着数据列未转换为数字
  • 尝试将数据列转换为浮点数,结果为TypeError: no numeric data to plot,它告诉您数据列包含ValueError: could not convert string to float: '?0.2',该列不能转换为浮点数。
    • '?'显示3行,其中df[df.min_temp.str.contains('\?')]
    • '?'将显示围绕问题行的数据框部分。
df[df.index.isin(pd.date_range('1982-07-17', '1982-07-24')) | df.index.isin(pd.date_range('1984-07-12', '1984-07-17'))]

enter image description here