zoukankan      html  css  js  c++  java
  • 数据分析学习笔记3------pandas操作excel

    读取Microsoft Excel文件

    In [104]: xlsx = pd.ExcelFile('examples/ex1.xlsx')
    In [105]: pd.read_excel(xlsx, 'Sheet1')
    Out[105]: 
       a   b   c   d message
    0  1   2   3   4   hello
    1  5   6   7   8   world
    2  9  10  11  12     foo
    

      

    In [106]: frame = pd.read_excel('examples/ex1.xlsx', 'Sheet1')
    
    In [107]: frame
    Out[107]: 
       a   b   c   d message
    0  1   2   3   4   hello
    1  5   6   7   8   world
    2  9  10  11  12     foo
    

    将pandas数据写入为Excel格式

    In [109]: frame.to_excel('examples/ex2.xlsx', 'Sheet1')
    
    In [110]: writer.save()
    

      

    Web APIs交互

    许多网站都有一些通过JSON或其他格式提供数据的公共API。通过Python访问这些API的办法有不少。一个简单易用的办法(推荐)是requests包(http://docs.python-requests.org)。

    In [113]: import requests
    
    In [114]: url = 'https://api.github.com/repos/pandas-dev/pandas/issues'
    
    In [115]: resp = requests.get(url)
    
    In [116]: resp
    Out[116]: <Response [200]>
    

    响应对象的json方法会返回一个包含被解析过的JSON字典,加载到一个Python对象中:

    In [117]: data = resp.json()
    
    In [118]: data[0]['title']
    Out[118]: 'Period does not round down for frequencies less that 1 hour'
    

    data中的每个元素都是一个包含所有GitHub主题页数据(不包含评论)的字典。我们可以直接传递数据到DataFrame,并提取感兴趣的字段:

    In [119]: issues = pd.DataFrame(data, columns=['number', 'title',
       .....:                                      'labels', 'state'])
    
    In [120]: issues
    

      

  • 相关阅读:
    梦断代码读后感02
    UML大战需求与分析--阅读笔记4
    UML大战需求分析--阅读笔记3
    软件需求与分析课堂讨论一
    UML大战需求分析--阅读笔记02
    UML大战需求分析--阅读笔记01
    学习进度--大三下
    问题账户需求分析
    2016年秋季阅读计划
    个人总结
  • 原文地址:https://www.cnblogs.com/helloluo/p/9739324.html
Copyright © 2011-2022 走看看