pandas 基础

Pandas 基础

文件的读取和写入

文件读取

pandas可以读取的文件格式有很多,主要介绍csv,excel,txt

import numpy as np
import pandas as pd
pd.__version__
'1.1.5'
df_csv = pd.read_csv('.\data\my_csv.csv', encoding='UTF-8')
df_csv
col1 col2 col3 col4 col5
0 2 a 1.4 apple 2020/1/1
1 3 b 3.4 banana 2020/1/2
2 6 c 2.5 orange 2020/1/5
3 5 d 3.2 lemon 2020/1/7
df_txt = pd.read_table('.\data\my_table.txt', encoding= 'UTF-8')
df_txt
col1 col2 col3 col4
0 2 a 1.4 apple 2020/1/1
1 3 b 3.4 banana 2020/1/2
2 6 c 2.5 orange 2020/1/5
3 5 d 3.2 lemon 2020/1/7
df_excel = pd.read_excel('.\data\my_excel.xlsx')
df_excel
col1 col2 col3 col4 col5
0 2 a 1.4 apple 2020/1/1
1 3 b 3.4 banana 2020/1/2
2 6 c 2.5 orange 2020/1/5
3 5 d 3.2 lemon 2020/1/7

这里有一些常用的公共参数,header=None 表示第一行不作为列名,index_col 表示把某一列或几列作为索
引,索引的内容将会在第三章进行详述,usecols 表示读取列的集合,默认读取所有的列,parse_dates 表示
需要转化为时间的列,关于时间序列的有关内容将在第十章讲解,nrows 表示读取的数据行数。上面这些参
数在上述的三个函数里都可以使用。

pd.read_table('.\data\my_table.txt', header = None)
0 1 2 3
0 col1 col2 col3 col4
1 2 a 1.4 apple 2020/1/1
2 3 b 3.4 banana 2020/1/2
3 6 c 2.5 orange 2020/1/5
4 5 d 3.2 lemon 2020/1/7
pd.read_csv('.\data\my_csv.csv', encoding='UTF-8', index_col=['col1', 'col2'])
col3 col4 col5
col1 col2
2 a 1.4 apple 2020/1/1
3 b 3.4 banana 2020/1/2
6 c 2.5 orange 2020/1/5
5 d 3.2 lemon 2020/1/7
pd.read_table('.\data\my_table.txt', usecols=['col1', 'col2'])
col1 col2
0 2 a
1 3 b
2 6 c
3 5 d
pd.read_table('.\data\my_table.txt', parse_dates =['col4'])
col1 col2 col3 col4
0 2 a 1.4 apple 2020/1/1
1 3 b 3.4 banana 2020/1/2
2 6 c 2.5 orange 2020/1/5
3 5 d 3.2 lemon 2020/1/7
pd.read_csv('data/my_csv.csv', parse_dates=['col5'])
col1 col2 col3 col4 col5
0 2 a 1.4 apple 2020-01-01
1 3 b 3.4 banana 2020-01-02
2 6 c 2.5 orange 2020-01-05
3 5 d 3.2 lemon 2020-01-07
pd.read_excel('.\data\my_excel.xlsx', nrows = 2)
col1 col2 col3 col4 col5
0 2 a 1.4 apple 2020/1/1
1 3 b 3.4 banana 2020/1/2

在读取txt 文件时,经常遇到分隔符非空格的情况,read_table 有一个分割参数sep ,它使得用户可以自定
义分割符号,进行txt 数据的读取。例如,下面的读取的表以|||| 为分割:

pd.read_table('data/my_table_special_sep.txt')
col1 |||| col2
0 TS |||| This is an apple.
1 GQ |||| My name is Bob.
2 WT |||| Well done!
3 PT |||| May I help you?
#上面的结果显然不是理想的,这时可以使用sep ,同时需要指定引擎为python :
pd.read_table('data/my_table_special_sep.txt', sep = '\|\|\|\|', engine='python') 

#在使用read_table 的时候需要注意,参数sep 中使用的是正则表达式,因此需要对| 进行转义
#变成\| ,否则无法读取到正确的结果。有关正则表达式的基本内容可以参考第八章或者其他相关资料

col1 col2
0 TS This is an apple.
1 GQ My name is Bob.
2 WT Well done!
3 PT May I help you?

数据写入

一般在数据写入中,最常用的操作是把index 设置为False ,特别当索引没有特殊意义的时候,这样的行为
能把索引在保存的时候去除。

df_csv.to_csv('data/my_csv_saved.csv', index=False)
df_excel.to_excel('data/my_excel_saved.xlsx', index=False)
#pandas 中没有定义to_table 函数,但是to_csv 可以保存为txt 文件,并且允许自定义分隔符,常用制表符\t 分割:
df_txt.to_csv('data/my_txt_saved.txt', sep='\t', index=False)
#如果想要把表格快速转换为markdown 和latex 语言,可以使用to_markdown 和to_latex 函数,此处需要安装tabulate 包。
print(df_csv.to_markdown())
|    |   col1 | col2   |   col3 | col4   | col5     |
|---:|-------:|:-------|-------:|:-------|:---------|
|  0 |      2 | a      |    1.4 | apple  | 2020/1/1 |
|  1 |      3 | b      |    3.4 | banana | 2020/1/2 |
|  2 |      6 | c      |    2.5 | orange | 2020/1/5 |
|  3 |      5 | d      |    3.2 | lemon  | 2020/1/7 |
print(df_csv.to_latex())
\begin{tabular}{lrlrll}
\toprule
{} &  col1 & col2 &  col3 &    col4 &      col5 \\
\midrule
0 &     2 &    a &   1.4 &   apple &  2020/1/1 \\
1 &     3 &    b &   3.4 &  banana &  2020/1/2 \\
2 &     6 &    c &   2.5 &  orange &  2020/1/5 \\
3 &     5 &    d &   3.2 &   lemon &  2020/1/7 \\
\bottomrule
\end{tabular}

基本数据结构

pandas 中具有两种基本的数据存储结构,存储一维values 的Series 和存储二维values 的DataFrame ,在
这两种结构上定义了很多的属性和方法。

Series

Series 一般由四个部分组成,分别是序列的值data 、索引index 、存储类型dtype 、序列的名字name 。其
中,索引也可以指定它的名字,默认为空。

s = pd.Series(data = [100, 'a', {
   'dic1':5}], 
              index = pd.Index(['id1', 20,'third'], name = 'my_idx'),
              dtype='object',
             name = 'my_name')
s
my_idx
id1              100
20                 a
third    {'dic1': 5}
Name: my_name, dtype: object

object 类型

object 代表了一种混合类型,正如上面的例子中存储了整数、字符串以及Python 的字典数据结
构。此外,目前pandas 把纯字符串序列也默认认为是一种object 类型的序列,但它也可以用
string 类型存储,文本序列的内容会在第八章中讨论。

# 对于这些属性,可以通过. 的方式来获取:
s.values
array([100, 'a', {'dic1': 5}], dtype=object)
s.index
Index(['id1', 20, 'third'], dtype='object', name='my_idx')
s.dtype
dtype('O')
s.name
'my_name'
#利用.shape 可以获取序列的长度:
s.shape
(3,)
s['third'] #索引取值
{'dic1': 5}

DataFrame

DataFrame 在Series 的基础上增加了列索引,一个数据框可以由二维的data 与行列索引来构造:

data = 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值