日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【解决方法】Panda read_csv()把第一行的数据变成了列名,怎么处理

發(fā)布時(shí)間:2025/4/16 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【解决方法】Panda read_csv()把第一行的数据变成了列名,怎么处理 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

有些時(shí)候,我們會(huì)遇到很多這樣的數(shù)據(jù),比如,這個(gè)csv的第一行并不是我們想象中的那樣是一個(gè)列名。那樣,我們處理數(shù)據(jù)的時(shí)候,就會(huì)出現(xiàn)問題,第一個(gè)不一致了嘛。


解決方案1

調(diào)用csv庫,自己重新編寫讀文件的程序。

csv庫,是python自帶的庫。

如果數(shù)據(jù)都是字符類型

這樣的條件下,問題是非常簡單,直接調(diào)用csv.reader()這個(gè)迭代器來讀取就好了。

如果數(shù)據(jù)中除了有字符串還有數(shù)字的話

下面我給一種解決的方法。

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

這個(gè)涉及到了之前的我寫過的一篇文章機(jī)器學(xué)習(xí)算法【感知機(jī)算法PLA】【5分鐘讀完】
在上面的這個(gè)代碼中,我需要讀取訓(xùn)練感知機(jī)的模型,但是發(fā)現(xiàn)給我的數(shù)據(jù)沒有列名,不想要改數(shù)據(jù),所以,就只有這么先封裝咯~
這個(gè)數(shù)據(jù)中,每一行的除了最后一列有可能是元素之外,其他都是浮點(diǎn)數(shù)。,所以,我就在這調(diào)用了float_test這個(gè)函數(shù),來做測試。

最后兩行,還有返回的那里是在做什么呢?其實(shí)就是,我想把最后一列給分出來,然后把其他恢復(fù)為一個(gè)二維的矩陣,每一行都是一個(gè)測試的X。


解決方法2

設(shè)置參數(shù)!!

參照pandas給出的read_csv這個(gè)函數(shù)的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.

關(guān)于names這個(gè)參數(shù)上說到,當(dāng)文件沒有涵蓋有header的話,那么你需要在header參數(shù)中明確指出!!

這個(gè)就是正確解釋,所以正確的操作是(**以需要讀取一個(gè)1.csv**文件為例)

import pandas as pddf = pd.read_csv('1.csv', header=None, names=['test'])

那么這個(gè)沒有列名的列就會(huì)被設(shè)置為test列~

感謝評論區(qū)大佬指出問題,已經(jīng)修改。

本文鏈接
【解決方法】Panda read_csv()把第一行的數(shù)據(jù)變成了列名,怎么處理

總結(jié)

以上是生活随笔為你收集整理的【解决方法】Panda read_csv()把第一行的数据变成了列名,怎么处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。