首页 > 编程语言 > 详细

Python高级数据处理与可视化(四)---- 数据存储

时间:2017-01-24 23:51:47      阅读:351      评论:0      收藏:0      [点我收藏+]

5. 数据存储

  5.1 CSV格式数据存取(Comma-Separated Values)

    5.1.1 将DataFrame保存到csv文件中:df.to_csv(‘xxx.csv‘)

技术分享
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 24 13:07:01 2017

@author: Wayne
"""

from matplotlib.finance import quotes_historical_yahoo_ochl
from datetime import date
import pandas as pd

today = date.today()
start = (today.year-1, today.month, today.day)
quotes = quotes_historical_yahoo_ochl(IBM,start,today)
quotesdf = pd.DataFrame(quotes)

quotesdf.to_csv(stockIBM.csv)
quotesdf.to_csv(‘stockIBM.csv‘)

    5.1.2 读取CSV文件:pd.read_csv(‘xxx.csv‘)

技术分享
In [2]:pd.read_csv(stockIBM.csv)
Out[2]: 
     Unnamed: 0         0           1           2           3           4           5  
0             0  735988.0  117.618075  117.598813  119.669894  117.174961  5446000.0   
1             1  735989.0  117.762571  118.090087  119.072651  117.752936  4617800.0
2             2  735990.0  118.224954  116.519922  118.841462  116.221304  5026400.0
...
251         251  736352.0  170.080002  171.029999  171.250000  170.009995  5327300.0
   
[252 rows x 7 columns]

In [3]:pd.read_csv(stockIBM.csv)[2]
Out[3]: 
0      117.598813
1      118.090087
2      116.519922
...
251    171.029999
Name: 2, dtype: float64
pd.read_csv(‘stockIBM.csv‘)

  5.2 XLS格式数据存取

    5.2.1 将DataFrame保存到xlsx文件中:quotesdf.to_excel(‘stockIBM.xlsx‘,sheet_name=‘IBM‘)

技术分享
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 24 13:07:01 2017

@author: Wayne
"""

from matplotlib.finance import quotes_historical_yahoo_ochl
from datetime import date
import pandas as pd

today = date.today()
start = (today.year-1, today.month, today.day)
quotes = quotes_historical_yahoo_ochl(IBM,start,today)
quotesdf = pd.DataFrame(quotes)

quotesdf.to_excel(stockIBM.xlsx,sheet_name=IBM)
quotesdf.to_excel(‘stockIBM.xlsx‘,sheet_name=‘IBM‘)

    5.2.2 读取XLSX文件:pd.read_excel(‘stockIBM.xlsx‘,sheet_name=‘IBM‘)

技术分享
In [7]: result = pd.read_excel(stockIBM.xlsx,sheet_name=IBM)

In [8]: result
Out[8]: 
          0           1           2           3           4         5
0    735988  117.618075  117.598813  119.669894  117.174961   5446000
1    735989  117.762571  118.090087  119.072651  117.752936   4617800
2    735990  118.224954  116.519922  118.841462  116.221304   5026400
...
251  736352  170.080002  171.029999  171.250000  170.009995   5327300

[252 rows x 6 columns]

In [9]: result[2]
Out[9]: 
0      117.598813
1      118.090087
2      116.519922
...
251    171.029999
Name: 2, dtype: float64
pd.read_excel(‘stockIBM.xlsx‘,sheet_name=‘IBM‘)

 

    

Python高级数据处理与可视化(四)---- 数据存储

原文:http://www.cnblogs.com/wnzhong/p/6348013.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!