将一个csv文件导入到Jupyter Notebook中的pandas

2024-04-25 04:17:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要以下方面的帮助:

我试图导入一个csv文件到我的Jupyter笔记本,但没有成功。在

我使用的代码是:

dfa = pd.read_csv('Filename.csv')

并给出以下错误消息:

   ---------------------------------------------------------------------------
ParserError                               Traceback (most recent call last)
<ipython-input-3-164d461fc4d7> in <module>()
----> 1 dfa = pd.read_csv('Airpollution.csv')

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    444 
    445     try:
--> 446         data = parser.read(nrows)
    447     finally:
    448         parser.close()

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/parsers.py in read(self, nrows)
   1034                 raise ValueError('skipfooter not supported for iteration')
   1035 
-> 1036         ret = self._engine.read(nrows)
   1037 
   1038         # May alter columns / col_dict

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/parsers.py in read(self, nrows)
   1846     def read(self, nrows=None):
   1847         try:
-> 1848             data = self._reader.read(nrows)
   1849         except StopIteration:
   1850             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 1 fields in line 4, saw 11

我已经检查过这些文件是从同一个文件夹打开的,并且它们都存储在我的桌面上。在

我安装了熊猫,matplotlib和seaborn。我尝试了所有的方法(从Stackoverflow的其他解决方案),但不明白为什么我不能导入。请开导我。谢谢您!在

-

@jpp: Another csv file was able to work 这是奇怪的,因为我试图使用另一个csv文件,它工作。我无法加载这些文件。在

我使用以下信息:

^{pr2}$

还有这个:

 Subject: Death and Life Expectancy 
 Topic : Death and Life Expectancy 
" Title  : M810131 - Deaths By Broad Groups Of Causes, Annual "
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,Number
 Variables , 1969 , 1970 , 1971 , 1972 , 1973 , 1974 , 1975 , 1976 , 1977 , 1978 , 1979 , 1980 , 1981 , 1982 , 1983 , 1984 , 1985 , 1986 , 1987 , 1988 , 1989 , 1990 , 1991 , 1992 , 1993 , 1994 , 1995 , 1996 , 1997 , 1998 , 1999 , 2000 , 2001 , 2002 , 2003 , 2004 , 2005 , 2006 , 2007 , 2008 , 2009 , 2010 , 2011 , 2012 , 2013 , 2014 , 2015 , 2016 , 2017 ,
 Total Deaths By Causes ," 10,224 "," 10,717 "," 11,329 "," 11,522 "," 11,920 "," 11,674 "," 11,447 "," 11,648 "," 11,955 "," 12,065 "," 12,468 "," 12,505 "," 12,863 "," 12,896 "," 13,321 "," 13,162 "," 13,348 "," 12,821 "," 13,173 "," 13,690 "," 14,069 "," 13,891 "," 13,876 "," 14,337 "," 14,461 "," 14,946 "," 15,569 "," 15,590 "," 15,305 "," 15,657 "," 15,516 "," 15,693 "," 15,367 "," 15,820 "," 16,036 "," 15,860 "," 16,215 "," 16,393 "," 17,140 "," 17,222 "," 17,101 "," 17,610 "," 18,027 "," 18,481 "," 18,938 "," 19,393 "," 19,862 "," 20,017 "," 20,905 ",
     Infective And Parasitic Diseases , 708 , 727 , 702 , 752 , 775 , 714 , 630 , 554 , 523 , 502 , 503 , 425 , 432 , 393 , 432 , 390 , 375 , 402 , 432 , 430 , 439 , 347 , 321 , 342 , 398 , 366 , 369 , 358 , 318 , 361 , 311 , 276 , 296 , 289 , 250 , 296 , 373 , 257 , 307 , 285 , 279 , 269 , 244 , 233 , 211 , 217 , 194 , 174 , 189 ,
         Tuberculosis , 419 , 458 , 439 , 489 , 450 , 472 , 420 , 358 , 340 , 318 , 331 , 240 , 221 , 207 , 224 , 163 , 177 , 177 , 186 , 168 , 132 , 113 , 104 , 101 , 115 , 101 , 118 , 132 , 115 , 128 , 107 , 101 , 104 , 92 , 79 , 79 , 67 , 66 , 85 , 83 , 75 , 77 , 68 , 65 , 51 , 60 , 41 , 41 , 32 ,
     Neoplasms ," 1,577 "," 1,633 "," 1,728 "," 1,821 "," 1,912 "," 2,002 "," 2,123 "," 2,278 "," 2,326 "," 2,415 "," 2,542 "," 2,623 "," 2,672 "," 2,729 "," 2,903 "," 2,817 "," 2,939 "," 2,921 "," 3,169 "," 3,233 "," 3,321 "," 3,314 "," 3,405 "," 3,497 "," 3,560 "," 3,785 "," 3,921 "," 4,034 "," 4,178 "," 4,091 "," 4,168 "," 4,278 "," 4,384 "," 4,465 "," 4,187 "," 4,353 "," 4,331 "," 4,722 "," 4,803 "," 5,081 "," 5,063 "," 5,078 "," 5,461 "," 5,651 "," 5,849 "," 5,790 "," 5,986 "," 5,993 "," 6,237 ",
         Malignant Neoplasms ," 1,533 "," 1,596 "," 1,688 "," 1,773 "," 1,863 "," 1,955 "," 2,083 "," 2,245 "," 2,286 "," 2,386 "," 2,488 "," 2,561 "," 2,616 "," 2,668 "," 2,858 "," 2,776 "," 2,893 "," 2,887 "," 3,131 "," 3,194 "," 3,283 "," 3,269 "," 3,361 "," 3,456 "," 3,531 "," 3,756 "," 3,898 "," 3,985 "," 4,128 "," 4,050 "," 4,134 "," 4,238 "," 4,339 "," 4,425 "," 4,146 "," 4,303 "," 4,289 "," 4,677 "," 4,745 "," 5,038 "," 5,010 "," 5,025 "," 5,411 "," 5,565 "," 5,775 "," 5,701 "," 5,903 "," 5,925 "," 6,077 ",
"     Endocrine, Nutritional And Metabolic Diseases ", 331 , 250 , 308 , 271 , 342 , 377 , 375 , 408 , 429 , 403 , 403 , 359 , 404 , 397 , 423 , 512 , 492 , 508 , 521 , 525 , 461 , 388 , 359 , 269 , 309 , 374 , 327 , 403 , 366 , 401 , 444 , 458 , 629 , 530 , 473 , 545 , 593 , 620 , 722 , 551 , 378 , 272 , 356 , 279 , 253 , 296 , 270 , 363 , 340 ,
         Diabetes , 184 , 134 , 212 , 207 , 247 , 257 , 259 , 334 , 377 , 334 , 347 , 319 , 368 , 361 , 373 , 469 , 464 , 479 , 492 , 501 , 419 , 332 , 320 , 238 , 264 , 334 , 271 , 320 , 282 , 308 , 350 , 355 , 512 , 425 , 373 , 474 , 510 , 536 , 609 , 463 , 290 , 182 , 299 , 268 , 247 , 277 , 250 , 343 , 321 ,
     Diseases Of The Blood And Blood-forming Organs , 71 , 51 , 60 , 50 , 61 , 60 , 52 , 32 , 50 , 45 , 41 , 31 , 42 , 33 , 33 , 28 , 29 , 30 , 35 , 35 , 48 , 50 , 40 , 33 , 34 , 24 , 37 , 37 , 44 , 35 , 50 , 54 , 52 , 44 , 39 , 33 , 40 , 36 , 31 , 46 , 30 , 41 , 41 , 20 , 14 , 23 , 10 , 14 , 17 ,
     Diseases Of The Nervous System And Sense Organs , 221 , 173 , 166 , 171 , 169 , 149 , 133 , 129 , 110 , 114 , 122 , 131 , 114 , 121 , 92 , 97 , 87 , 87 , 102 , 133 , 111 , 143 , 117 , 127 , 93 , 71 , 89 , 89 , 95 , 110 , 105 , 107 , 122 , 94 , 67 , 81 , 68 , 62 , 64 , 75 , 68 , 92 , 117 , 166 , 137 , 144 , 210 , 226 , 185 ,
     Diseases Of The Circulatory System ," 2,733 "," 2,899 "," 3,120 "," 2,999 "," 3,169 "," 3,295 "," 3,369 "," 3,798 "," 3,889 "," 3,983 "," 4,233 "," 4,305 "," 4,413 "," 4,430 "," 4,436 "," 4,637 "," 4,651 "," 4,482 "," 4,675 "," 4,847 "," 5,082 "," 5,152 "," 5,070 "," 5,270 "," 5,315 "," 5,460 "," 5,560 "," 5,896 "," 5,680 "," 5,711 "," 5,810 "," 5,749 "," 5,588 "," 5,401 "," 5,727 "," 5,423 "," 5,397 "," 5,441 "," 5,835 "," 5,794 "," 5,611 "," 5,807 "," 5,720 "," 5,747 "," 5,765 "," 5,987 "," 6,101 "," 6,107 "," 6,541 ",
         Heart And Hypertensive Diseases ," 1,761 "," 1,780 "," 1,925 "," 1,819 "," 1,967 "," 2,014 "," 2,000 "," 2,283 "," 2,426 "," 2,518 "," 2,752 "," 2,777 "," 2,892 "," 2,866 "," 2,911 "," 3,156 "," 3,129 "," 3,028 "," 3,251 "," 3,318 "," 3,416 "," 3,385 "," 3,234 "," 3,457 "," 3,552 "," 3,653 "," 3,742 "," 3,984 "," 3,943 "," 3,950 "," 4,061 "," 3,976 "," 4,075 "," 3,856 "," 4,067 "," 3,714 "," 3,656 "," 3,833 "," 4,197 "," 4,201 "," 4,081 "," 4,161 "," 3,920 "," 3,848 "," 3,914 "," 4,165 "," 4,534 "," 4,576 "," 4,970 ",
         Cerebrovascular Disease , 863 ," 1,038 "," 1,103 "," 1,080 "," 1,131 "," 1,213 "," 1,244 "," 1,427 "," 1,360 "," 1,382 "," 1,409 "," 1,447 "," 1,438 "," 1,469 "," 1,454 "," 1,413 "," 1,418 "," 1,355 "," 1,343 "," 1,414 "," 1,551 "," 1,666 "," 1,700 "," 1,697 "," 1,652 "," 1,692 "," 1,701 "," 1,805 "," 1,645 "," 1,633 "," 1,633 "," 1,625 "," 1,409 "," 1,393 "," 1,556 "," 1,562 "," 1,616 "," 1,462 "," 1,490 "," 1,435 "," 1,375 "," 1,472 "," 1,628 "," 1,714 "," 1,680 "," 1,620 "," 1,357 "," 1,317 "," 1,310 ",
     Diseases Of The Respiratory System ," 1,235 "," 1,473 "," 1,502 "," 1,653 "," 1,663 "," 1,631 "," 1,632 "," 1,651 "," 1,902 "," 1,724 "," 2,024 "," 1,965 "," 2,196 "," 2,257 "," 2,429 "," 2,096 "," 2,241 "," 1,974 "," 1,942 "," 2,110 "," 2,167 "," 2,112 "," 2,289 "," 2,522 "," 2,588 "," 2,564 "," 2,912 "," 2,534 "," 2,385 "," 2,579 "," 2,357 "," 2,505 "," 2,239 "," 2,763 "," 2,992 "," 2,851 "," 3,124 "," 2,913 "," 2,948 "," 2,989 "," 3,188 "," 3,434 "," 3,493 "," 3,708 "," 4,061 "," 4,232 "," 4,417 "," 4,440 "," 4,757 ",
         Pneumonia , 655 , 843 , 875 , 951 , 950 , 969 , 948 ," 1,010 "," 1,215 ", 942 ," 1,124 "," 1,129 "," 1,284 "," 1,375 "," 1,513 "," 1,204 "," 1,287 "," 1,082 ", 998 ," 1,039 "," 1,130 "," 1,191 "," 1,285 "," 1,420 "," 1,596 "," 1,670 "," 2,028 "," 1,693 "," 1,553 "," 1,780 "," 1,641 "," 1,794 "," 1,540 "," 2,079 "," 2,340 "," 2,232 "," 2,437 "," 2,244 "," 2,375 "," 2,387 "," 2,614 "," 2,766 "," 2,879 "," 3,096 "," 3,512 "," 3,680 "," 3,859 "," 3,855 "," 4,212 ",
     Diseases Of The Digestive System , 402 , 454 , 463 , 463 , 453 , 451 , 423 , 384 , 382 , 359 , 382 , 368 , 385 , 400 , 403 , 369 , 394 , 326 , 329 , 380 , 363 , 374 , 406 , 353 , 361 , 394 , 409 , 416 , 357 , 418 , 412 , 326 , 307 , 339 , 383 , 356 , 385 , 384 , 392 , 377 , 351 , 436 , 426 , 414 , 418 , 482 , 477 , 467 , 485 ,
     Diseases Of The Genito-urinary System , 234 , 239 , 252 , 279 , 275 , 320 , 311 , 281 , 324 , 381 , 349 , 366 , 366 , 319 , 375 , 405 , 319 , 343 , 393 , 380 , 370 , 346 , 369 , 362 , 371 , 444 , 483 , 444 , 399 , 494 , 470 , 486 , 487 , 594 , 587 , 641 , 634 , 637 , 739 , 753 , 861 , 893 , 918 , 934 , 967 , 951 , 928 , 913 , 925 ,
     Congenital Anomalies , 181 , 150 , 186 , 172 , 189 , 177 , 146 , 156 , 141 , 185 , 184 , 185 , 178 , 182 , 155 , 172 , 189 , 202 , 171 , 201 , 170 , 189 , 164 , 163 , 160 , 148 , 157 , 130 , 108 , 112 , 95 , 85 , 79 , 69 , 59 , 49 , 67 , 70 , 55 , 60 , 60 , 60 , 53 , 54 , 47 , 50 , 62 , 72 , 49 ,
         Congenital Anomalies Of Heart , 84 , 76 , 102 , 93 , 94 , 101 , 76 , 70 , 70 , 98 , 105 , 111 , 109 , 101 , 86 , 91 , 84 , 101 , 87 , 98 , 75 , 84 , 82 , 92 , 94 , 90 , 89 , 74 , 68 , 57 , 48 , 48 , 33 , 40 , 32 , 28 , 38 , 42 , 40 , 32 , 36 , 35 , 21 , 25 , 21 , 26 , 32 , 38 , 22 ,
     Certain Causes Of Perinatal Mortality , 460 , 463 , 455 , 502 , 477 , 322 , 254 , 221 , 247 , 239 , 261 , 227 , 208 , 215 , 149 , 151 , 147 , 128 , 128 , 127 , 135 , 123 , 89 , 82 , 76 , 68 , 51 , 64 , 61 , 62 , 52 , 48 , 24 , 52 , 41 , 22 , 39 , 43 , 32 , 39 , 49 , 34 , 49 , 44 , 43 , 42 , 30 , 36 , 39 ,
"     Accidents, Poisonings And Violence ", 811 , 836 , 968 , 982 , 995 , 894 , 887 , 890 , 914 ," 1,057 ", 876 , 899 , 938 , 966 ," 1,085 "," 1,095 "," 1,082 "," 1,025 ", 931 , 958 ," 1,042 "," 1,008 "," 1,074 "," 1,127 "," 1,066 "," 1,122 "," 1,113 "," 1,040 "," 1,187 "," 1,110 "," 1,066 "," 1,133 "," 1,036 "," 1,053 "," 1,062 "," 1,028 "," 1,017 "," 1,027 "," 1,036 "," 1,006 ", 978 , 973 , 989 ," 1,030 ", 933 , 909 , 895 , 890 , 840 ,
         Suicides , 188 , 185 , 230 , 235 , 240 , 229 , 252 , 257 , 224 , 266 , 249 , 271 , 191 , 239 , 267 , 211 , 327 , 329 , 302 , 367 , 395 , 354 , 319 , 298 , 296 , 347 , 401 , 271 , 346 , 371 , 309 , 348 , 357 , 361 , 346 , 381 , 405 , 419 , 374 , 364 , 401 , 353 , 361 , 467 , 422 , 415 , 409 , 429 , 361 ,
         Transport Accidents , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , na , 199 , 232 , 226 , 201 , 208 , 207 , 192 , 176 , 183 , 168 , 164 , 141 ,
     Other Diseases And Causes ," 1,260 "," 1,369 "," 1,419 "," 1,407 "," 1,440 "," 1,282 "," 1,112 ", 866 , 718 , 658 , 548 , 621 , 515 , 454 , 406 , 393 , 403 , 393 , 345 , 331 , 360 , 345 , 173 , 190 , 130 , 126 , 141 , 145 , 127 , 173 , 176 , 188 , 124 , 127 , 167 , 182 , 147 , 181 , 176 , 166 , 185 , 221 , 160 , 201 , 240 , 270 , 282 , 322 , 301 ,



"Deaths prior to 1979 are classified according to the eighth (1965) revision of the International Classification of Diseases.  Deaths from 1979 to 2011 are classified according to the ninth (1975) revision.  From 2012, deaths are classified according to the tenth revision."

SOURCE: REGISTRY OF BIRTHS AND DEATHS




Generated by: SingStat Table Builder 
Date generated: 05/09/2018
Contact: info@singstat.gov.sg 

我不太确定这是否与文件或我的mac电脑的设置有关。。谢谢您!在


Tags: andofcsvthetoinselfparser
2条回答

您可以跳过错误的行(字段数不匹配):

dfa = pd.read_csv('Filename.csv',error_bad_lines=False) 

您应该考虑使用^{}可用的参数。例如,可以指定分隔符和跳过行。在末尾有一个空列,在底部有一个垃圾,但这可以在读取文件之后处理。在

例如:

df = pd.read_csv('file.csv', sep=' *, *', skiprows=4, engine='python')\
       .dropna(subset=['2007'])\
       .iloc[:, :-1]

print(df)

                                            Variables   2007   2008   2009  \
0   Sulphur Dioxide (Annual Mean) (Microgram Per C...   12.0   11.0    9.0   
1   Sulphur Dioxide (Maximum 24-hour Mean) (Microg...   84.0   80.0   93.0   
2   Nitrogen Dioxide (Annual Mean) (Microgram Per ...   22.0   22.0   22.0   
3   Nitrogen Dioxide (Maximum 1-hour Mean) (Microg...  177.0  126.0  147.0   
4   Particulate Matter (PM10) (Annual Mean) (Micro...   27.0   25.0   29.0   
5   Particulate Matter (PM10) (99th Percentile 24-...   53.0   49.0   59.0   
6   Particulate Matter (PM2.5) (Annual Mean) (Micr...   19.0   16.0   19.0   
7   Particulate Matter (PM2.5) (99th Percentile 24...   37.0   32.0   44.0   
8   Carbon Monoxide (Maximum 8-hour Mean) (Milligr...    1.7    1.6    1.9   
9   Carbon Monoxide (Maximum 1-hour Mean) (Milligr...    2.5    2.3    3.9   
10  Ozone (Maximum 8-hour Mean) (Microgram Per Cub...  206.0  183.0  105.0   

     2010   2011   2012   2013   2014   2015   2016  
0    11.0   10.0   13.0   14.0   12.0   12.0   13.0  
1   104.0   80.0   98.0   75.0   83.0   75.0   61.0  
2    23.0   25.0   25.0   25.0   24.0   22.0   26.0  
3   153.0  189.0  154.0  132.0  121.0   99.0  123.0  
4    26.0   27.0   29.0   31.0   30.0   37.0   26.0  
5    76.0   55.0   57.0  215.0   75.0  186.0   61.0  
6    17.0   17.0   19.0   20.0   18.0   24.0   15.0  
7    56.0   41.0   42.0  176.0   51.0  145.0   40.0  
8     2.4    2.0    1.9    5.5    1.8    3.3    2.2  
9     2.8    2.6    2.4    7.5    2.7    3.5    2.7  
10  139.0  123.0  122.0  139.0  135.0  152.0  115.0  

相关问题 更多 >