資料的導入
首先是導入csv檔案
#coding:utf-8
import pandas as pd
df = pd.read_csv(r'/Users/herenyi/Downloads/4/4.1/1.csv', encoding = 'UTF-8' )
其次是導入txt檔案
df1 = pd.read_table(r'/Users/herenyi/Downloads/4/4.1/2.txt', names = ['age', 'name' ], sep = ',', encoding = 'UTF-8')
最後是導入excel檔案,如果預設第一行為列名
df2 = pd.read_excel(r'/Users/herenyi/Downloads/4/4.1/3.xls', sheetname = 'data')
如果沒有列名,則要自行導入列名,用names參數輸入
df2 = pd.read_excel(r'/Users/herenyi/Downloads/4/4.1/3.xls', sheetname = 'data', header = None, names = ['age', 'name'])
資料的導出
這裡我們以常用的csv檔案為例,首先我們先輸入個Dataframe資料結構的資料集
df = pd.DataFrame({
'age': [21, 22, 23],
'name': ['KEN', 'John', 'JIMI']
})
然後我們導出這個df,主要有filePath,sep,index,和header四個參數,如果我們不做任何更改,隻需如下導出即可,下面是我存放的路徑。
df.to_csv(r'/Users/herenyi/Desktop/test/df.csv')
如果你不要行序列,則隻需要把index參數改成False即可。
df.to_csv(r'/Users/herenyi/Desktop/test/df.csv', index = False);