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

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')

   5.3 练习:

    已知有excel表格内容(请自行创建):

    

    1.创建一个DataFrame将已有表格内容读出并存入此DataFrame中;

    2.添加字段Sum_score,其值为相应学生的Python和Math两门课程分数之和;

    3.将更新过的DataFrame中的内容写回students.xlsx文件的scores工作表中。

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

@author: Wayne
"""

import pandas as pd

df = pd.read_excel('test.xlsx')
df['sum'] = df['Python'] + df['Math']
df.to_excel('test.xlsx',sheet_name='scores')
df['sum'] = df['Python'] + df['Math']

 

    

转载于:https://www.cnblogs.com/wnzhong/p/6348013.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值