CSV文件如下:
報錯①:
取csv文件語句:
df = pd.read_csv(file_path ,skip_blank_lines=True,
header=None)
報錯:
b'Skipping line 7: expected 2 fields, saw 3
剛開始沒有注意這個錯誤,只是想要讀取某一行的時候一直顯示讀不到。
後來看了這個報錯,才發現只能讀取前兩列。有的答案是讓你忽略掉讀不到的行。但是讀不到的行恰恰對我很重要。
最終的解決方案:(尋找最大列數)
file_list = os.listdir(r"C:\Users\740773\Downloads\New folder\RFS003")
#讀取採樣週期和數據
for file_name in file_list:
file_path = r"C:\Users\740773\Downloads\New folder\RFS003" + "\\" + file_name
with open(file_path) as temp_f:
# get No of columns in each line
col_count = [len(l.split(",")) for l in temp_f.readlines()]
column_names = [i for i in range(max(col_count))]
df = pd.read_csv(file_path ,skip_blank_lines=True,
header=None, names=column_names)
感謝!!!!
報錯②:
import matplotlib.pyplot as plt
報錯:
ImportError: DLL load failed: The specified module could not be found.
解決方法:安裝了之後就解決了。
感謝!!!
問題③:
想用iloc讀取特定行列的值並用取得的值做數值運算
代碼:
Sampling_Rate = df.iloc[3, 1]
Sampling_Period = df.iloc[[4,1]]
x = np.arange(0, Sampling_Period, int(Sampling_Period)/int(Sampling_Rate))
顯示數據類型爲:<class 'pandas.core.frame.DataFrame'>
把iloc改爲“iat”可以解決問題。