前言
有些時候,我們會遇到很多這樣的資料,比如,這個csv的第一行并不是我們想象中的那樣是一個列名。那樣,我們處理資料的時候,就會出現問題,第一個不一緻了嘛。
解決方案1
調用csv庫,自己重新編寫讀檔案的程式。
csv庫,是python自帶的庫。
如果資料都是字元類型
這樣的條件下,問題是非常簡單,直接調用csv.reader()這個疊代器來讀取就好了。
如果資料中除了有字元串還有數字的話
下面我給一種解決的方法。
def float_test(data: str):
try:
return float(data)
except Exception:
return data
def read(filename):
"""
:param filename:
:return:
"""
values = []
with open(filename) as f:
r = csv.reader(f)
for row in r:
values.append(list(map(float_test, row)))
*data, label = list(map(list, zip(*values)))
return list(zip(*data)), label
這個涉及到了之前的我寫過的一篇文章機器學習算法【感覺機算法PLA】【5分鐘讀完】
在上面的這個代碼中,我需要讀取訓練感覺機的模型,但是發現給我的資料沒有列名,不想要改資料,是以,就隻有這麼先封裝咯~
這個資料中,每一行的除了最後一列有可能是元素之外,其他都是浮點數。,是以,我就在這調用了float_test這個函數,來做測試。
最後兩行,還有傳回的那裡是在做什麼呢?其實就是,我想把最後一列給分出來,然後把其他恢複為一個二維的矩陣,每一行都是一個測試的X。
解決方法2
設定參數!!
參照pandas給出的read_csv這個函數的API解釋:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html
其中有句話講到了:
- header : int or list of ints, default ‘infer’
- Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of integers that specify row locations for a multi-index on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file.
- names : array-like, default None
- List of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list will cause a UserWarning to be issued.
關于names這個參數上說到,當檔案沒有涵蓋有header的話,那麼你需要在header參數中明确指出!!
這個就是正确解釋,是以正确的操作是(**以需要讀取一個
1.csv
**檔案為例)
import pandas as pd
df = pd.read_csv('1.csv', header=None, names=['test'])
那麼這個沒有列名的列就會被設定為test列~
感謝評論區大佬指出問題,已經修改。
本文連結
【解決方法】Panda read_csv()把第一行的資料變成了列名,怎麼處理