python讀取csv文件中帶有小數點的的文件?
讀取文件:
f= pd.read_csv('文件名', encoding='gb2312')
讀取文件中某些列的數據
data = f6.loc[:, [“列名1”,“列名2”]]
假設data有這幾列{“a”,“b”,“c”}
如果a這一列的數據是{60ml,250ml,250ml,60ml,250ml,250ml,60ml,60ml,250ml,250ml,60ml,}
要只取{60,250,250,60,250,250,60,60,250,250,60,}則可以直接寫成:
data["a"]= data["a"].str.extract('(\d+)', expand=False)
data["a"].astype(int)
如果a這一列數據有小數如{8.86℃,8.86℃,8.86℃,8.86℃,8.86℃},如果有負數則正則改為:-?\d+(?:.\d+)?
data["a"]= data["a"].str.extract('(\d+(?:\.\d+)?)', expand=False)
data["a"].astype(floa
如果某一列是百分數如{55%,63%,72%,52%,72%}
data["a"]= data["a"].str.extract('(\d+(?:\.\d+)?)', expand=False)
data["a"].astype(float)=data["a"].astype(float)*0.01
這樣就可以得到{0.55,0.63,0.72,0.52,0.72}