Python3数据科学汇总: https://blog.csdn.net/weixin_41793113/article/details/99707225
数据分析过程中经常需要进行读写操作,Pandas实现了很多 IO 操作的API,这里简单做了一个列举。
格式类型 | 数据描述 | Reader | Writer |
---|---|---|---|
text | CSV | read_ csv | to_csv |
text | JSON | read_json | to_json |
text | HTML | read_html | to_html |
text | clipboard | read_clipboard | to_clipboard |
binary | Excel | read_excel | to_excel |
binary | HDF5 | read_hdf | to_hdf |
binary | Feather | read_feather | to_feather |
binary | Msgpack | read_msgpack | to_msgpack |
binary | Stata | read_stata | to_stata |
binary | SAS | read_sas | |
binary | Python Pickle | read_pickle | to_pickle |
SQL | SQL | read_sql | to_sql |
SQLGoogle | Big Query | read_gbq | to_gbq |
主要内容
- 文件读取
- 1.read_csv
- 2.read_excel
- 3.read_html
- 4.read_sql
- 5.read_sql_table
- 文件保存
- 1.to_csv
- 2.to_excel
- 3.to_sql
#常规导包操作
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import webbrowser
webbrowser 模块的 open()函数可以启动一个新浏览器,打开指定的 URL。在交互式环境中输入以下代码:
import webbrowser
webbrowser.open('http://inventwithpython.com/')
link = 'http://pandas.pydata.org/pandas-docs/version/0.20/io.html'
webbrowser.open(link) ## 打开上面的url链接
df1 = pd.read_clipboard() #读取粘贴板的内容,赋值给df1
df1 #查看df1
df1.to_clipboard() #把df1的内容放到粘贴板中
可以Ctrl+V验证下
df1.to_csv('df1.csv', index=False) #默认index=true False表示不要打印索引
默认存放在同级目录下,文件名为df1.csv,../df1,csv则表示存在上一级目录下
ls #也可以通过ls看看文件夹有没生成
more df1.csv #可以通过more xx.csv 查看里面的内容
df2 = pd.read_csv('df1.csv') #读入df1.csv的内容到df2,默认是带索引的
df1.to_json() #里面什么都没是直接以json格式显示,df1.to_json('df1.json')则会保存,其他同理
pd.read_json(df1.to_json()) #上面的数据太难看了,pandas有内置函数查看
不一一展示了。里面没有东西就显示出来,里面是路径+文件名则生成