DataFrame讀取寫入檔案的方法
阿新 • • 發佈:2018-11-10
DataFrame有非常豐富的IO方法,比如DataFrame讀寫csv檔案excel檔案等等,操作很簡單。下面在程式碼中標記出來一些常用的讀寫操作方法,需要的時候查詢一下該方法就可以了。
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)
df1 = pd.read_clipboard()
print(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
'''
df1.to_csv('df1.csv') # 預設會把 index 也當成一列寫入到檔案中
df1.to_csv('df2.csv', index=False) # 如果不想顯示索引,可以新增第二個引數 index = False
print(df1.to_json())
df1.to_excel('df1.xlsx')