【解决方法】Panda read_csv()把第一行的数据变成了列名,怎么处理
前言
有些時候,我們會遇到很多這樣的數據,比如,這個csv的第一行并不是我們想象中的那樣是一個列名。那樣,我們處理數據的時候,就會出現問題,第一個不一致了嘛。
解決方案1
調用csv庫,自己重新編寫讀文件的程序。
csv庫,是python自帶的庫。
如果數據都是字符類型
這樣的條件下,問題是非常簡單,直接調用csv.reader()這個迭代器來讀取就好了。
如果數據中除了有字符串還有數字的話
下面我給一種解決的方法。
def float_test(data: str):try:return float(data)except Exception:return datadef 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 pddf = pd.read_csv('1.csv', header=None, names=['test'])那么這個沒有列名的列就會被設置為test列~
感謝評論區大佬指出問題,已經修改。
本文鏈接
【解決方法】Panda read_csv()把第一行的數據變成了列名,怎么處理
總結
以上是生活随笔為你收集整理的【解决方法】Panda read_csv()把第一行的数据变成了列名,怎么处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【多进程并行版本】爬取链家二手房前100
- 下一篇: 长度限制的队列Python