您的位置:首页 > 编程语言 > Python开发

莫烦 python Pandas 学习笔记 2017.12.7.11

2017-12-07 11:45 453 查看

Pandas 导入导出

源地址https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/3-5-pd-to/

读取 .csv 文件

- 示范档案下载 student.csv https://pan.baidu.com/s/1eScY2f4[百度地址]

实验环境:

Ubuntu 16.04 LTS

Jupyter notebook

Python 3.5

- Ubuntu 文件 复制

sudo mv student.csv ~/pandas_learn


- 莫烦 Python 源码

原文代码:

import pandas as pd #加载模块

#读取csv
data = pd.read_csv('students.csv')

#打印出data
print(data)


直接抄写代码会出现:

--------------------------------------------------
FileNotFoundErrorTraceback (most recent call last)
<ipython-input-6-08b8bb9e90fd> in <module>()
1 import pandas as pd
2
----> 3 data = pd.read_csv('students.csv')
4
5 print(data)

/usr/local/lib/python3.5/dist-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, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
653                     skip_blank_lines=skip_blank_lines)
654
--> 655         return _read(filepath_or_buffer, kwds)
656
657     parser_f.__name__ = name

/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
403
404     # Create the parser.
--> 405     parser = TextFileReader(filepath_or_buffer, **kwds)
406
407     if chunksize or iterator:

/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
762             self.options['has_index_names'] = kwds['has_index_names']
763
--> 764         self._make_engine(self.engine)
765
766     def close(self):

/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in _make_engine(self, engine)
983     def _make_engine(self, engine='c'):
984         if engine == 'c':
--> 985             self._engine = CParserWrapper(self.f, **self.options)
986         else:
987             if engine == 'python':

/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
1603         kwds['allow_leading_cols'] = self.index_col is not False
1604
-> 1605         self._reader = parsers.TextReader(src, **kwds)
1606
1607         # XXX

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__ (pandas/_libs/parsers.c:4209)()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source (pandas/_libs/parsers.c:8873)()

FileNotFoundError: File b'students.csv' does not exist


不要被这么长的报错吓到 ,直接看最后一行

FileNotFoundError: File b'students.csv' does not exist


没有找到文件
students.csv


再看 莫烦 Python 下载的文件名是
student.csv


- FileNotFoundError: File b’students.csv’ does not exist 解决方案

1. 修改 csv 文件名为
students

sudo mv student.csv sudents.csv


2. 修改代码

import pandas as pd

data = pd.read_csv('student.csv')

print(data)


运行结果

Student ID  name   age  gender
0         1100  Kelly   22  Female
1         1101    Clo   21  Female
2         1102  Tilly   22  Female
3         1103   Tony   24    Male
4         1104  David   20    Male
5         1105  Catty   22  Female
6         1106      M    3  Female
7         1107      N   43    Male
8         1108      A   13    Male
9         1109      S   12    Male
10        1110  David   33    Male
11        1111     Dw    3  Female
12        1112      Q   23    Male
13        1113      W   21  Female
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python ubuntu pandas