前言
本文是给使用pandas的新手而写,主要列出一些常见的问题,根据笔者所踩过的坑,进行归纳总结,希望对读者有所帮助。
示例文件
将以下内容保存为文件 people.csv。
id,姓名,性别,出生日期,出生地,职业,爱好
1,张小三,m,1992-10-03,北京,工程师,足球
2,李云义,m,1995-02-12,上海,程序员,读书 下棋
3,周娟,女,1998-03-25,合肥,护士,音乐,跑步
4,赵盈盈,Female,2001-6-32,,学生,画画
5,郑强强,男,1991-03-05,南京(nanjing),律师,历史-政治
如果一切正常的话,在Jupyter Notebook 中应该显示以下内容:
文件编码
文件编码格式是最容易出错的问题之一。如果编码格式不正确,就会完全读取不出文件内容,出现类似于以下的错误, 让人完全不知所措:
—————————————————————————
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-6-8659adefcfa6> in <module>
—-> 1 pd.read_csv(‘people.csv’, encoding=’gb2312′)C:\ProgramData\Anaconda3\lib\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, 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)
683 )
684
–> 685 return _read(filepath_or_buffer, kwds)
686
687 parser_f.__name__ = nameC:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
455
456 # Create the parser.
–> 457 parser = TextFileReader(fp_or_buf, **kwds)
458
459 if chunksize or iterator:C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
本文来源gaodai#ma#com搞*!代#%^码网5 893 self.options[“has_index_names”] = kwds[“has_index_names”]
894
–> 895 self._make_engine(self.engine)
896
897 def close(self):