数据分析常用库之【pandas】读取保存加载功能

read_to

#python的pickle模块实现了基本的数据序列和反序列化。通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,

#永久存储;通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象。

总是会看到代码开头会加上from __future__ import *这样的语句。这样的做法的作用就是将新版本的特性引进当前版本中,也就是说我们可以在当前版本使用新版本的一些特性。

print用法:

# python 2.x
from __future__ import print_function
print("Hello World")

division用法 

# python 2.x
5/2
>>> 2
 
from __future__ import division
5/2
>>> 2.5

with用法

# python 2.x
try:
    with open('test.txt', 'w') as f:
    f.write('Hello World')
finally:
    f.close()
 
# 用with替代上述异常检测代码:
from __future__ import with_statement
with open('test.txt', 'w') as f:
    f.write('Hi there!')

绝对引入

主要针对2.4版本之前

from __future__ import absolute_import

 pandas读取csv文件

from __future__ import print_function
import pandas as pd
# Read from
data = pd.read_csv('C:\\Users\Public\\Documents\\python_study\\前置代码code\\code\\a1_python_lib\\1_pandas\\0_read_to\\student.csv',engine='python')
print('1')
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'''

 

 保存文件为pickle格式

# Save to
data.to_pickle('C:\\Users\Public\\Documents\\python_study\\前置代码code\\code\\a1_python_lib\\1_pandas\\0_read_to\\student.pickle')

将pickle格式的文件加载出来

# Load pickle
import pprint, pickle
pkl_file = open('C:\\Users\Public\\Documents\\python_study\\前置代码code\\code\\a1_python_lib\\1_pandas\\0_read_to\\student.pickle', 'rb')
data1 = pickle.load(pkl_file)
print('2')
pprint.pprint(data1)
'''
    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'''

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值