簡體   English   中英

如何將“臟”csv 文件導入 pandas 和 plot?

[英]How to import a 'dirty' csv file into pandas and plot?

這是我試過的代碼。 .csv 文件有一列包含日期 (dd-mm-yyyy)。我想創建一個這樣的圖表。 這是僅包含 2 列的.csv文件!

    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

我得到的錯誤是:沒有數字數據到 plot 這一定是因為.csv 文件只有 2 列(日期、溫度)& 因為它無法讀取日期,它只考慮 1 列 & 給了我錯誤。

請幫我顯示日期,因為我從這個網站得到了 .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


  • 數據有一些問題需要清理
  • 文件末尾的額外文本行,可以使用skiprows跳過
  • 嘗試 plot 數據導致TypeError: no numeric data to plot ,這意味着數據列未轉換為數字
  • 嘗試將數據列轉換為浮點數,導致ValueError: could not convert string to float: '?0.2' ,這表明數據列包含一個'?' ,無法轉換為浮點數。
    • df[df.min_temp.str.contains('\?')]顯示 3 行帶有'?'
    • 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'))] 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'))]將顯示問題行周圍的 dataframe 部分。
import pandas as pd

# read the file, parse dates and set as index, skip rows with issues
df = pd.read_csv('data/archive/daily-minimum-temperatures-in-me.csv', parse_dates=['Date'], skiprows=[3652, 3653], index_col=['Date'])

# rename column
df.columns = ['min_temp']

# replace strings in the column and then convert to floats
df.min_temp = df.min_temp.str.replace('?', '').astype(float)

# plot the dataframe
df.plot(figsize=(8, 5))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM