Python与数据科学实战课程——第三章Pandas:Dataframe_IO

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

读写到粘贴板中

import webbrowser
link = "http://pandas.pydata.org/pandas-docs/version/0.20/io.html"
webbrowser.open(link)
True
df1 = pd.read_clipboard()
df1
Format Type Data Description Reader Writer
0 text CSV read_csv to_csv
1 text JSON read_json to_json
2 text HTML read_html to_html
3 text Local clipboard read_clipboard to_clipboard
4 binary MS Excel read_excel to_excel
5 binary HDF5 Format read_hdf to_hdf
6 binary Feather Format read_feather to_feather
7 binary Msgpack read_msgpack to_msgpack
8 binary Stata read_stata to_stata
9 binary SAS read_sas
10 binary Python Pickle Format read_pickle to_pickle
11 SQL SQL read_sql to_sql
12 SQL Google Big Query read_gbq to_gbq
import sys #查看Python版本
print (sys.version) 
print (sys.version_info)

3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 12:30:02) [MSC v.1900 64 bit (AMD64)]
sys.version_info(major=3, minor=6, micro=2, releaselevel=‘final’, serial=0)

df1.to_clipboard   #写入粘贴板,用于复制到Excel里

读写到CSV中

csv的写入

df1.to_csv("df1.csv")
import os
os.listdir() 

[’.ipynb_checkpoints’,
‘df1.csv’,
‘df1.html’,
‘df1.xlsx’,
‘one_array.npy’,
‘two_array.npz’,
‘x.pkl’,
‘第三章pandas:Dataframe.ipynb’,
‘第三章Pandas:Dataframe_IO .ipynb’,
‘第三章Pandas:Series.ipynb’,
‘第三章Pandas:深入理解series和dataframe.ipynb’,
‘第二章numpy:array的input和output.ipynb’,
‘第二章numpy:数组与矩阵运算.ipynb’,
‘第二章numpy:数组创建和访问.ipynb’]

!more df1.csv     #查看写入后的文件内容,可以看到添加了index这个column,但这个实际上是不需要的

,Format Type,Data Description,Reader,Writer
0,text,CSV,read_csv,to_csv
1,text,JSON,read_json,to_json
2,text,HTML,read_html,to_html
3,text,Local clipboard,read_clipboard,to_clipboard
4,binary,MS Excel,read_excel,to_excel
5,binary,HDF5 Format,read_hdf,to_hdf
6,binary,Feather Format,read_feather,to_feather
7,binary,Msgpack,read_msgpack,to_msgpack
8,binary,Stata,read_stata,to_stata
9,binary,SAS,read_sas,?
10,binary,Python Pickle Format,read_pickle,to_pickle
11,SQL,SQL,read_sql,to_sql
12,SQL,Google Big Query,read_gbq,to_gbq

df1.to_csv("df1.csv",index=False)
!more df1.csv

Format Type,Data Description,Reader,Writer
text,CSV,read_csv,to_csv
text,JSON,read_json,to_json
text,HTML,read_html,to_html
text,Local clipboard,read_clipboard,to_clipboard
binary,MS Excel,read_excel,to_excel
binary,HDF5 Format,read_hdf,to_hdf
binary,Feather Format,read_feather,to_feather
binary,Msgpack,read_msgpack,to_msgpack
binary,Stata,read_stata,to_stata
binary,SAS,read_sas,?
binary,Python Pickle Format,read_pickle,to_pickle
SQL,SQL,read_sql,to_sql
SQL,Google Big Query,read_gbq,to_gbq

csv的读取

df2 = pd.read_csv("df1.csv")
df2
Format Type Data Description Reader Writer
0 text CSV read_csv to_csv
1 text JSON read_json to_json
2 text HTML read_html to_html
3 text Local clipboard read_clipboard to_clipboard
4 binary MS Excel read_excel to_excel
5 binary HDF5 Format read_hdf to_hdf
6 binary Feather Format read_feather to_feather
7 binary Msgpack read_msgpack to_msgpack
8 binary Stata read_stata to_stata
9 binary SAS read_sas ?
10 binary Python Pickle Format read_pickle to_pickle
11 SQL SQL read_sql to_sql
12 SQL Google Big Query read_gbq to_gbq

dataframe和json的转换

df1.to_json()

‘{“Format Type”:{“0”:“text”,“1”:“text”,“2”:“text”,“3”:“text”,“4”:“binary”,“5”:“binary”,“6”:“binary”,“7”:“binary”,“8”:“binary”,“9”:“binary”,“10”:“binary”,“11”:“SQL”,“12”:“SQL”},“Data Description”:{“0”:“CSV”,“1”:“JSON”,“2”:“HTML”,“3”:“Local clipboard”,“4”:“MS Excel”,“5”:“HDF5 Format”,“6”:“Feather Format”,“7”:“Msgpack”,“8”:“Stata”,“9”:“SAS”,“10”:“Python Pickle Format”,“11”:“SQL”,“12”:“Google Big Query”},“Reader”:{“0”:“read_csv”,“1”:“read_json”,“2”:“read_html”,“3”:“read_clipboard”,“4”:“read_excel”,“5”:“read_hdf”,“6”:“read_feather”,“7”:“read_msgpack”,“8”:“read_stata”,“9”:“read_sas”,“10”:“read_pickle”,“11”:“read_sql”,“12”:“read_gbq”},“Writer”:{“0”:“to_csv”,“1”:“to_json”,“2”:“to_html”,“3”:“to_clipboard”,“4”:“to_excel”,“5”:“to_hdf”,“6”:“to_feather”,“7”:“to_msgpack”,“8”:“to_stata”,“9”:"\u00a0",“10”:“to_pickle”,“11”:“to_sql”,“12”:“to_gbq”}}’

pd.read_json(df1.to_json())
Data Description Format Type Reader Writer
0 CSV text read_csv to_csv
1 JSON text read_json to_json
10 Python Pickle Format binary read_pickle to_pickle
11 SQL SQL read_sql to_sql
12 Google Big Query SQL read_gbq to_gbq
2 HTML text read_html to_html
3 Local clipboard text read_clipboard to_clipboard
4 MS Excel binary read_excel to_excel
5 HDF5 Format binary read_hdf to_hdf
6 Feather Format binary read_feather to_feather
7 Msgpack binary read_msgpack to_msgpack
8 Stata binary read_stata to_stata
9 SAS binary read_sas

dataframe和html的转换

df1.to_html()
'\n  \n    \n      \n      \n      \n      \n      \n    \n  \n  \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n    \n      \n      \n      \n      \n      \n    \n  \n
Format TypeData DescriptionReaderWriter
0textCSVread_csvto_csv
1textJSONread_jsonto_json
2textHTMLread_htmlto_html
3textLocal clipboardread_clipboardto_clipboard
4binaryMS Excelread_excelto_excel
5binaryHDF5 Formatread_hdfto_hdf
6binaryFeather Formatread_featherto_feather
7binaryMsgpackread_msgpackto_msgpack
8binaryStataread_statato_stata
9binarySASread_sas
10binaryPython Pickle Formatread_pickleto_pickle
11SQLSQLread_sqlto_sql
12SQLGoogle Big Queryread_gbqto_gbq
'
df1.to_html("df1.html")
os.listdir() 

[’.ipynb_checkpoints’,
‘df1.csv’,
‘df1.html’,
‘one_array.npy’,
‘two_array.npz’,
‘x.pkl’,
‘第三章pandas:Dataframe.ipynb’,
‘第三章Pandas:Dataframe_IO .ipynb’,
‘第三章Pandas:Series.ipynb’,
‘第三章Pandas:深入理解series和dataframe.ipynb’,
‘第二章numpy:array的input和output.ipynb’,
‘第二章numpy:数组与矩阵运算.ipynb’,
‘第二章numpy:数组创建和访问.ipynb’]

查看HTML

Python与数据科学实战课程——第三章Pandas:Dataframe_IO_第1张图片

dataframe写Excel文件

df1.to_excel("df1.xlsx")
!ls

驱动器 D 中的卷是 LENOVO
卷的序列号是 AEED-1D16

 D:\Python_DS_Project 的目录

2020-08-12  09:44              .
2020-08-12  09:44              ..
2020-08-09  17:06              .ipynb_checkpoints
2020-08-10  13:58               509 df1.csv
2020-08-10  17:08             2,057 df1.html
2020-08-10  17:06             5,479 df1.xlsx
2020-08-08  09:49               120 one_array.npy
2020-08-08  10:08               474 two_array.npz
2020-08-08  09:46               195 x.pkl
2020-08-09  10:38            28,721 第三章pandas:Dataframe.ipynb
2020-08-12  09:44            27,022 第三章Pandas:Dataframe_IO .ipynb
2020-08-08  17:43            11,474 第三章Pandas:Series.ipynb
2020-08-09  11:28            17,656 第三章Pandas:深入理解series和dataframe.ipynb
2020-08-08  10:16             7,691 第二章numpy:array的input和output.ipynb
2020-08-07  16:39            18,123 第二章numpy:数组与矩阵运算.ipynb
2020-08-07  16:18            10,928 第二章numpy:数组创建和访问.ipynb
              13 个文件        130,449 字节
               3 个目录 81,778,208,768 可用字节

你可能感兴趣的:(实战网课,python)