pandas & csv

Pandas

官方文件

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_numpy.html#pandas.DataFrame.to_numpy

1.讀取csv : data=pandas.read_csv('filename')

2.瀏覽資料內容 : data.head()

3.讀取特定欄位資料(列,直直的) : detail=data[['name1','name2']......]

3-1. 轉為一般陣列: data['name'].tolist()

3-2. 可用numpy將series或者dataframe型態轉為numpy.array

4.計算各欄位的多種統計資訊(count,mean,std,min,max,25%,50%,75%) : data.describe()

5.所有欄位名稱: data.colums

6.直接選取各筆資料 data[1:] (list切割方式)

Source: pandas 資料處理

iloc

data.iloc[2] --> 第三個row (第三筆資料,橫的)

data.iloc[2:,0:2] --> 切割區塊(row,colums),抓取第一筆之後所有資料,但只包含第一、二項特徵

data.iloc[1:4,:] --> 以row選取

data.iloc[:,1:4] --> 以colums選取

iloc[0] --> Series

iloc[[0]] --> DataFrame

Source:Python Pandas DataFrame 的 iloc[0] 與 iloc[[0]]

讀取

https://tinycorner.tw/2018/04/24/使用python讀取csv和xlsx檔/

https://tinycorner.tw/2018/04/24/使用python讀取csv和xlsx檔/

處理空值

判斷nan

四捨五入

https://blog.csdn.net/shuibuzhaodeshiren/article/details/84324885

Series轉換

https://medium.com/@weilihmen/python-pandas-%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C%E6%95%99%E5%AD%B8-%E6%88%90%E7%B8%BE%E8%A1%A8-f6d0ec4f89

轉置矩陣

http://violin-tao.blogspot.com/2017/06/pandas-1-indexing.html

row

用loop讀取

https://thispointer.com/pandas-6-different-ways-to-iterate-over-rows-in-a-dataframe-update-while-iterating-row-by-row/

直接轉成numpy array

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_numpy.html#pandas.DataFrame.to_numpy

colum

https://www.delftstack.com/zh-tw/howto/python-pandas/how-to-delete-pandas-dataframe-column/

dataframe轉dict

可是value會是list ([0])

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_dict.html

https://stackoverflow.com/questions/26716616/convert-a-pandas-dataframe-to-a-dictionary

日期轉換

https://t.codebug.vip/questions-1537063.htm

CSV

1.讀取csv : inputfile=open('filepath','r')

2.讀取內容 : data=csv.reader(inputfile) (list, 包含欄位名稱(第一項[0]))

Source : CSV讀寫

Python之xlsx檔案與csv檔案相互轉換

Workbook轉csv

https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/434360/

https://blog.csdn.net/qq_33689414/article/details/78307031

https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/369201/

issue: 輸出csv多一行空白

https://blog.csdn.net/pfm685757/article/details/47806469

openpyxl

https://www.geeksforgeeks.org/python-adjusting-rows-and-columns-of-an-excel-file-using-openpyxl-module/

https://www.itread01.com/content/1544850004.html

自動調整寬度尚未測試

Last updated

Was this helpful?