Python之Pandas库学习(二):数据读写

1. I/O API工具

读取函数写入函数
read_csvto_csv
read_excelto_excel
read_hdfto_hdf
read_sqlto_sql
read_jsonto_json
read_htmlto_html
read_statato_stata
read_clipboardto_clipboard
read_pickleto_pickle
read_msgpackto_mspack
read_gbqto_gbq

2. 读写CSV文件

文件的每一行的元素是用逗号隔开,这种格式的文件就叫CSV文件。

2.1. 从CSV中读取数据

  1. 简单读取

    excited.csv

    white,read,blue,green,animal
    1,5,2,3,cat
    2,7,8,5,dog
    3,3,6,7,horse
    2,2,8,3,duck
    4,4,2,1,mouse

    code.py

    >>> csvframe = pd.read_csv('E:\\Python\\Codes\\excited.csv')
    >>> csvframe
       white  read  blue  green animal
    0      1     5     2      3    cat
    1      2     7     8      5    dog
    2      3     3     6      7  horse
    3      2     2     8      3   duck
    4      4     4     2      1  mouse
  2. 用header和names指定表头

    excited.csv

    1,5,2,3,cat
    2,7,8,5,dog
    3,3,6,7,horse
    2,2,8,3,duck
    4,4,2,1,mouse

    code.py

    >>> csvframe = pd.read_csv('E:\\Python\\Codes\\excited.csv', header=None)
    >>> csvframe
       0  1  2  3      4
    0  1  5  2  3    cat
    1  2  7  8  5    dog
    2  3  3  6  7  horse
    3  2  2  8  3   duck
    4  4  4  2  1  mouse
    
    >>> csvframe = pd.read_csv('E:\\Python\\Codes\\excited.csv', names=['white', 'red', 'blue', 'green', 'animal'])
    >>> csvframe
       white  red  blue  green animal
    0      1    5     2      3    cat
    1      2    7     8      5    dog
    2      3    3     6      7  horse
    3      2    2     8      3   duck
    4      4    4     2      1  mouse
  3. 创建等级结构的DataFrame

    excited.csv

    color,status,item1,item2,item3
    black,up,3,4,6
    black,down,2,6,7
    white,up,5,5,5
    white,down,3,3,2
    white,left,1,2,1
    red,up,2,2,2
    red,down,1,1,4

    code.py

    >>> csvframe = pd.read_csv('E:\\Python\\Codes\\excited.csv', index_col=['color', 'status'])
    >>> csvframe
                  item1  item2  item3
    color status                     
    black up          3      4      6
          down        2      6      7
    white up          5      5      5
          down        3      3      2
          left        1      2      1
    red   up          2      2      2
          down        1      1      4

2.2. 写入数据到CSV中

  1. 简单写入

    code.py

    >>> frame = pd.DataFrame(np.arange(16).reshape((4,4)), columns = ['red', 'blue', 'orange', 'black'], index = ['a', 'b', 'c', 'd'])
    >>> frame
       red  blue  orange  black
    a    0     1       2      3
    b    4     5       6      7
    c    8     9      10     11
    d   12    13      14     15
    >>> frame.to_csv('E:\\Python\\Codes\\excited.csv')

    excited.csv

    ,red,blue,orange,black
    a,0,1,2,3
    b,4,5,6,7
    c,8,9,10,11
    d,12,13,14,15

    可以发现第一行的前面有一个',',因为列名前面有一个空白。

  2. 取消索引和列的写入

    code.py

    >>> frame.to_csv('E:\\Python\\Codes\\excited.csv', index = False, header = False)

    excited.csv

    0,1,2,3
    4,5,6,7
    8,9,10,11
    12,13,14,15
  3. 处理NaN元素

    code.py

    >>> frame = pd.DataFrame([[3, 2, np.NaN], [np.NaN, np.NaN, np.NaN], [2, 3, 3]], index = ['a', 'b', 'c'], columns = ['red', 'black', 'orange'])
    >>> frame
       red  black  orange
    a  3.0    2.0     NaN
    b  NaN    NaN     NaN
    c  2.0    3.0     3.0
    >>> frame.to_csv('E:\\Python\\Codes\\excited.csv')
    
    使用np_rep参数把空字段替换
    >>> frame.to_csv('E:\\Python\\Codes\\excited.csv', na_rep = 'lalala')

    excited.csv

    ,red,black,orange
    a,3.0,2.0,
    b,,,
    c,2.0,3.0,3.0
    可以发现所有的NaN就是为空的
    
    替换
    ,red,black,orange
    a,3.0,2.0,lalala
    b,lalala,lalala,lalala
    c,2.0,3.0,3.0
    这里发现列首的第一个还是没有东西,因为它本身不存在?

3. 读写TXT文件

TXT文件不一定是以逗号或者分号分割数据的,这种时候要用正则表达式。通常还要配合'*'号表示匹配任意多个。

例如'\s*'.

符号意义
.换行符以外的单个字符
\d数字
\D非数字字符
\s空白字符
\S非空白字符
\n换行符
\t制表符
\uxxxx用十六进制数字xxxx表示的Unicode字符
  1. 简单读取

    excited.txt

    乱加空格和制表符
    white red blue green
     1   5 2 3
    2 7  8   5
    2 3 3 3

    code.py

    >>> pd.read_table('E:\\Python\\Codes\\excited.txt', sep = '\s*')
    __main__:1: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
    E:\Python\Python3\lib\site-packages\pandas\io\parsers.py:2137: FutureWarning: split() requires a non-empty pattern match.
      yield pat.split(line.strip())
    E:\Python\Python3\lib\site-packages\pandas\io\parsers.py:2139: FutureWarning: split() requires a non-empty pattern match.
      yield pat.split(line.strip())
       white  red  blue  green
    0      1    5     2      3
    1      2    7     8      5
    2      2    3     3      3
    第一次尝试的时候报错了,于是按照提示加上
    
    >>> pd.read_table('E:\\Python\\Codes\\excited.txt', sep = '\s*', engine = 'python')
       white  red  blue  green
    0      1    5     2      3
    1      2    7     8      5
    2      2    3     3      3
    成功了,其中'*'号的意思是匹配任意多个
  2. 读取时排除一些行

    excited.txt

    12#$@!%$!$#!@$!@$!@
    #$%^$^%$#!
    @#%!
    white red blue green
    !$#$!@$#!@$
     1   5 2 3
    2 7  8   5
    2 3 3 3
    ^&##$^@FGSDQAS

    code.py

    >>> pd.read_table('E:\\Python\\Codes\\excited.txt', sep = '\s*', engine = 'python', skiprows = [0, 1, 2, 4, 8])
       white  red  blue  green
    0      1    5     2      3
    1      2    7     8      5
    2      2    3     3      3
    列表内代表要跳过的行
  3. 读取部分数据

    sep也可以用在read_csv啊原来。nrows代表读取几行的数据,例如nrows=3那么就读取3行的数据。

    chunksize是把文件分割成一块一块的,chunksize=3的话就是每一块的行数为3.

    excited.txt

    white red blue green black orange golden
     1   5 2 3 111 222 233
    100 7    8   5 2333 23333 233333
    20 3 3 3 12222 1222 23232
    2000 7   8   5 2333 23333 233333
    300 3 3 3 12222 1222 23232

    code.py

    >>> frame = pd.read_csv('E:\\Python\\Codes\\excited.txt', sep = '\s*', skiprows=[2], nrows = 3, engine = 'python')
    >>> frame
       white  red  blue  green  black  orange  golden
    0      1    5     2      3    111     222     233
    1     20    3     3      3  12222    1222   23232
    2   2000    7     8      5   2333   23333  233333
    从头开始读三行,并且跳过了第三行
    
    >>> pieces = pd.read_csv('E:\\Python\\Codes\\excited.txt', sep = '\s*', chunksize = 2, engine = 'python')
    >>> for piece in pieces:
    ...   print (piece)
    ...   print (type(piece))
    ... 
       white  red  blue  green  black  orange  golden
    0      1    5     2      3    111     222     233
    1    100    7     8      5   2333   23333  233333
    <class 'pandas.core.frame.DataFrame'>
       white  red  blue  green  black  orange  golden
    2     20    3     3      3  12222    1222   23232
    3   2000    7     8      5   2333   23333  233333
    <class 'pandas.core.frame.DataFrame'>
       white  red  blue  green  black  orange  golden
    4    300    3     3      3  12222    1222   23232
    <class 'pandas.core.frame.DataFrame'>
    每两个为一块。并且类型都是DataFrame。

3.2. 写入数据到TXT中

写入数据的话和csv是一样的。

4. 读写HTML文件

4.1. 写入数据到HTML文件中

先看看to_html()方法

code.py

>>> frame
   white  red  blue  green  black  orange  golden
0      1    5     2      3    111     222     233
1    100    7     8      5   2333   23333  233333
2     20    3     3      3  12222    1222   23232
3   2000    7     8      5   2333   23333  233333
4    300    3     3      3  12222    1222   23232
>>> print(frame.to_html())
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>white</th>
      <th>red</th>
      <th>blue</th>
      <th>green</th>
      <th>black</th>
      <th>orange</th>
      <th>golden</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>5</td>
      <td>2</td>
      <td>3</td>
      <td>111</td>
      <td>222</td>
      <td>233</td>
    </tr>
    <tr>
      <th>1</th>
      <td>100</td>
      <td>7</td>
      <td>8</td>
      <td>5</td>
      <td>2333</td>
      <td>23333</td>
      <td>233333</td>
    </tr>
    <tr>
      <th>2</th>
      <td>20</td>
      <td>3</td>
      <td>3</td>
      <td>3</td>
      <td>12222</td>
      <td>1222</td>
      <td>23232</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2000</td>
      <td>7</td>
      <td>8</td>
      <td>5</td>
      <td>2333</td>
      <td>23333</td>
      <td>233333</td>
    </tr>
    <tr>
      <th>4</th>
      <td>300</td>
      <td>3</td>
      <td>3</td>
      <td>3</td>
      <td>12222</td>
      <td>1222</td>
      <td>23232</td>
    </tr>
  </tbody>
</table>

可以发现DataFrame.to_html()可以将DataFrame直接变成html的表格内容。因此我们要把一个DataFrame变成可以浏览的html文件的时候,只需要插入一些其他的东西。

code.py

>>> s = ['<HTML>']
>>> s.append('<HEAD><TITLE>DataFrame</TITLE></HEAD>')
>>> s.append('<BODY>')
>>> s.append(frame.to_html())
>>> s.append('</BODY></HTML>')
>>> html = ''.join(s)
>>> html_file = open('E:\\Python\\Codes\\DataFrame.html', 'w')
>>> html_file.write(html)
1193
>>> html_file.close()

DataFrame.html

whiteredbluegreenblackorangegolden
01523111222233
1100785233323333233333
22033312222122223232
32000785233323333233333
430033312222122223232

4.2. 从HTML文件中读取数据

read_html()方法会返回页面所有的表格,因此得到的是一个DataFrame数组。

code.py

从上例读取
>>> web_frames = pd.read_html('E:\\Python\\Codes\\DataFrame.html')
>>> for web_frame in web_frames:
...   print (web_frame)
... 
   Unnamed: 0  white  red  blue  green  black  orange  golden
0           0      1    5     2      3    111     222     233
1           1    100    7     8      5   2333   23333  233333
2           2     20    3     3      3  12222    1222   23232
3           3   2000    7     8      5   2333   23333  233333
4           4    300    3     3      3  12222    1222   23232

最厉害的是,read_html()可以以网址作为参数,直接解析并抽取网页中的表格。

于是试了试百度百科四谎的剧集

code.py

>>> favors = pd.read_html('http://baike.baidu.com/item/%E5%9B%9B%E6%9C%88%E6%98%AF%E4%BD%A0%E7%9A%84%E8%B0%8E%E8%A8%80/13382872#viewPageContent')
>>> now = favors[0].copy()
>>> now = now.set_index(0)
>>> now.columns = now.ix['话']
>>> now.index.name = None
>>> now.drop('话')
话             标题(日/中)               剧本  \
1    モノトーン・カラフル 单调·多彩          吉 冈 孝 夫   
2             友人A 友人A             石黑恭平   
3             春の中 春光里              神户守   
4              旅立ち 启程  岩田和也 河野亚矢子 石黑恭平   
5          どんてんもよう 阴天             石滨真史   
6              帰り道 归途             井端义秀   
7         カゲささやく 暗影低语              神户守   
8               响け 回响             后藤圭二   
9               共鸣 共鸣              神户守   
10     君といた景色 与你共赏的景色             中村章子   
11           命の灯 生命之光             朝仓海斗   
12  トゥインクル リトルスター 小星星              神户守   
13         爱の悲しみ 爱的忧伤             仓田绫子   
14              足迹 足迹             柴山智隆   
15            うそつき 骗子              神户守   
16        似たもの同士 相似的人             黑木美幸   
17          トワイライト 暮光              神户守   
18          心重ねる 心心相印             石井俊匡   
19     さよならヒーロー 再见了英雄             井端义秀   
20            手と手 手与手              神户守   
21                雪 雪        仓田绫子 柴山智隆   
22              春风 春风             石黑恭平   
23            MOMENTS             岩田和也   

话                                         分镜  \
1                                       石黑恭平   
2                                       原田孝宏   
3                                       岩田和也   
4   三木俊明 河合拓也 牧田昌也 野野下伊织 山田慎也 菅井爱明 小泉初荣 浅贺和行   
5                                  石滨真史 小岛崇史   
6                                      野野下伊织   
7                                       间岛崇宽   
8                                       高桥英俊   
9                                       黑木美幸   
10                                      原田孝宏   
11                                 石黑恭平 川越崇弘   
12                                      福岛利规   
13                                     野野下伊织   
14                                      小泉初荣   
15                                       矢岛武   
16                 山田真也 野野下伊织 小泉初荣 三木俊明 浅贺和行   
17                                     河野亚矢子   
18                                      河合拓也   
19                                       こさや   
20                                       矢岛武   
21            野野下伊织 小泉初荣 门之园惠美 高野绫 河合拓也 山田真也   
22                                 石黑恭平 黑木美幸   
23                      爱敬由纪子 奥田佳子 山田真也 伊藤香织   

话                                                  演出       作画监督 演奏 作画监督 总作画监督  
1                                               爱敬由纪子       浅贺和行       -   NaN  
2                                           三木俊明 小林惠祐      爱敬由纪子     NaN   NaN  
3                                                河合拓也        NaN     NaN   NaN  
4                                           浅贺和行 仓田绫子  爱敬由纪子 高野绫     NaN   NaN  
5                                                小岛崇史          -   爱敬由纪子   NaN  
6                                                浅贺和行        NaN     NaN   NaN  
7                                                山田真也          -     NaN   NaN  
8                                                河合拓也       浅贺和行     NaN   NaN  
9                                                小泉初荣        NaN     NaN   NaN  
10                                                高野绫        NaN     NaN   NaN  
11                                           山下惠 中野彰子          -     NaN   NaN  
12                                               长森佳容       浅贺和行     NaN   NaN  
13                                                NaN        NaN     NaN   NaN  
14                                                  -        NaN     NaN   NaN  
15                  北岛勇树 山下惠 C Company NAMU Animation       浅贺和行     NaN   NaN  
16                                                  -        高野绫     NaN   NaN  
17                                           三木俊明 高田晃       浅贺和行   爱敬由纪子   NaN  
18                                                NaN        NaN     NaN   NaN  
19                           小泉初荣 野野下伊织 高野绫 山田真也 河合拓也        NaN     NaN   NaN  
20  野野下伊织 小泉初荣 河合拓也 山田真也 高野绫 薗部爱子 奥田佳子 加藤万由子 高田晃 薮本和彦        NaN     NaN   NaN  
21                                                NaN        NaN     NaN   NaN  
22       奥田桂子 河合拓也 野野下伊织 高野绫 小泉初荣 伊藤香织 浅贺和行 高田晃 爱敬由纪子        NaN     NaN   NaN  
23                                                NaN        NaN     NaN   NaN  

很强大。但是因为外移了一行..搞了挺久终于完美显示了。

5. 其他格式

除了表列出来的文件格式,还有HDF5格式、pickle格式等。

转载于:https://www.cnblogs.com/fightfordream/p/7071346.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值