欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用pandas讀取csv文件的指定列方法

 更新時(shí)間:2018年04月21日 08:48:09   作者:grey_csdn  
下面小編就為大家分享一篇使用pandas讀取csv文件的指定列方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

根據(jù)教程實(shí)現(xiàn)了讀取csv文件前面的幾行數(shù)據(jù),一下就想到了是不是可以實(shí)現(xiàn)前面幾列的數(shù)據(jù)。經(jīng)過多番嘗試總算試出來了一種方法。

之所以想實(shí)現(xiàn)讀取前面的幾列是因?yàn)槲沂诸^的一個(gè)csv文件恰好有后面幾列沒有可用數(shù)據(jù),但是卻一直存在著。原來的數(shù)據(jù)如下:

GreydeMac-mini:chapter06 greyzhang$ cat data.csv

1,name_01,coment_01,,,,
2,name_02,coment_02,,,,
3,name_03,coment_03,,,,
4,name_04,coment_04,,,,
5,name_05,coment_05,,,,
6,name_06,coment_06,,,,
7,name_07,coment_07,,,,
8,name_08,coment_08,,,,
9,name_09,coment_09,,,,
10,name_10,coment_10,,,,
11,name_11,coment_11,,,,
12,name_12,coment_12,,,,
13,name_13,coment_13,,,,
14,name_14,coment_14,,,,
15,name_15,coment_15,,,,
16,name_16,coment_16,,,,
17,name_17,coment_17,,,,
18,name_18,coment_18,,,,
19,name_19,coment_19,,,,
20,name_20,coment_20,,,,
21,name_21,coment_21,,,,

如果使用pandas讀取出全部的數(shù)據(jù),打印的時(shí)候會(huì)出現(xiàn)以下結(jié)果:

In [41]: data = pd.read_csv('data.csv')

In [42]: data
Out[42]: 
  1 name_01 coment_01 Unnamed: 3 Unnamed: 4 Unnamed: 5 Unnamed: 6
0 2 name_02 coment_02   NaN   NaN   NaN   NaN
1 3 name_03 coment_03   NaN   NaN   NaN   NaN
2 4 name_04 coment_04   NaN   NaN   NaN   NaN
3 5 name_05 coment_05   NaN   NaN   NaN   NaN
4 6 name_06 coment_06   NaN   NaN   NaN   NaN
5 7 name_07 coment_07   NaN   NaN   NaN   NaN
6 8 name_08 coment_08   NaN   NaN   NaN   NaN
7 9 name_09 coment_09   NaN   NaN   NaN   NaN
8 10 name_10 coment_10   NaN   NaN   NaN   NaN
9 11 name_11 coment_11   NaN   NaN   NaN   NaN
10 12 name_12 coment_12   NaN   NaN   NaN   NaN
11 13 name_13 coment_13   NaN   NaN   NaN   NaN
12 14 name_14 coment_14   NaN   NaN   NaN   NaN
13 15 name_15 coment_15   NaN   NaN   NaN   NaN
14 16 name_16 coment_16   NaN   NaN   NaN   NaN
15 17 name_17 coment_17   NaN   NaN   NaN   NaN
16 18 name_18 coment_18   NaN   NaN   NaN   NaN
17 19 name_19 coment_19   NaN   NaN   NaN   NaN
18 20 name_20 coment_20   NaN   NaN   NaN   NaN
19 21 name_21 coment_21   NaN   NaN   NaN   NaN

所說在學(xué)習(xí)的過程中這并不會(huì)給我?guī)硎裁凑系K,但是在命令行終端界面呆久了總喜歡稍微清爽一點(diǎn)的風(fēng)格。使用read_csv的參數(shù)usecols能夠在一定程度上減少這種混亂感。

In [45]: data = pd.read_csv('data.csv',usecols=[0,1,2,3])

In [46]: data
Out[46]: 
  1 name_01 coment_01 Unnamed: 3
0 2 name_02 coment_02   NaN
1 3 name_03 coment_03   NaN
2 4 name_04 coment_04   NaN
3 5 name_05 coment_05   NaN
4 6 name_06 coment_06   NaN
5 7 name_07 coment_07   NaN
6 8 name_08 coment_08   NaN
7 9 name_09 coment_09   NaN
8 10 name_10 coment_10   NaN
9 11 name_11 coment_11   NaN
10 12 name_12 coment_12   NaN
11 13 name_13 coment_13   NaN
12 14 name_14 coment_14   NaN
13 15 name_15 coment_15   NaN
14 16 name_16 coment_16   NaN
15 17 name_17 coment_17   NaN
16 18 name_18 coment_18   NaN
17 19 name_19 coment_19   NaN
18 20 name_20 coment_20   NaN
19 21 name_21 coment_21   NaN

為了能夠看到數(shù)據(jù)的“邊界”,讀取的時(shí)候顯示了第一列無效的數(shù)據(jù)。正常的使用中,或許我們是想連上面結(jié)果中最后一列的信息也去掉的,那只需要在參數(shù)重去掉最后一列的列號(hào)。

In [47]: data = pd.read_csv('data.csv',usecols=[0,1,2])

In [48]: data
Out[48]: 
  1 name_01 coment_01
0 2 name_02 coment_02
1 3 name_03 coment_03
2 4 name_04 coment_04
3 5 name_05 coment_05
4 6 name_06 coment_06
5 7 name_07 coment_07
6 8 name_08 coment_08
7 9 name_09 coment_09
8 10 name_10 coment_10
9 11 name_11 coment_11
10 12 name_12 coment_12
11 13 name_13 coment_13
12 14 name_14 coment_14
13 15 name_15 coment_15
14 16 name_16 coment_16
15 17 name_17 coment_17
16 18 name_18 coment_18
17 19 name_19 coment_19
18 20 name_20 coment_20
19 21 name_21 coment_21

以上這篇使用pandas讀取csv文件的指定列方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python使用QQ郵箱發(fā)送郵件報(bào)錯(cuò)smtplib.SMTPAuthenticationError

    Python使用QQ郵箱發(fā)送郵件報(bào)錯(cuò)smtplib.SMTPAuthenticationError

    這篇文章主要介紹了Python使用QQ郵箱發(fā)送郵件報(bào)錯(cuò)smtplib.SMTPAuthenticationError,簡(jiǎn)單介紹了python 發(fā)送郵件的步驟,需要的朋友可以參考下
    2019-12-12
  • Python中函數(shù)eval和ast.literal_eval的區(qū)別詳解

    Python中函數(shù)eval和ast.literal_eval的區(qū)別詳解

    eval函數(shù)在Python中做數(shù)據(jù)類型的轉(zhuǎn)換還是很有用的。它的作用就是把數(shù)據(jù)還原成它本身或者是能夠轉(zhuǎn)化成的數(shù)據(jù)類型。那么eval和ast.literal_val()的區(qū)別是什么呢?本文將大家介紹關(guān)于Python中函數(shù)eval和ast.literal_eval區(qū)別的相關(guān)資料,需要的朋友可以參考下。
    2017-08-08
  • 詳解Python中正則匹配TAB及空格的小技巧

    詳解Python中正則匹配TAB及空格的小技巧

    這篇文章主要介紹了詳解Python中正則匹配TAB及空格的小技巧,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python之virtualenv的簡(jiǎn)單使用方法(必看篇)

    python之virtualenv的簡(jiǎn)單使用方法(必看篇)

    下面小編就為大家分享一python之virtualenv的簡(jiǎn)單使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • Python類的基本寫法與注釋風(fēng)格介紹

    Python類的基本寫法與注釋風(fēng)格介紹

    這篇文章主要介紹了Python類的基本寫法與注釋風(fēng)格,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Python閉包技巧介紹

    Python閉包技巧介紹

    這篇文章主要介紹了Python閉包,所謂閉包就是用函數(shù)代替類,被外層函數(shù)包圍的內(nèi)層函數(shù),它能夠獲取外層函數(shù)范圍中的變量,感興趣的小伙伴請(qǐng)和小編一起進(jìn)入文章學(xué)習(xí)具體內(nèi)容吧
    2021-12-12
  • Python2.7基于淘寶接口獲取IP地址所在地理位置的方法【測(cè)試可用】

    Python2.7基于淘寶接口獲取IP地址所在地理位置的方法【測(cè)試可用】

    這篇文章主要介紹了Python2.7基于淘寶接口獲取IP地址所在地理位置的方法,涉及Python調(diào)用淘寶IP庫接口進(jìn)行IP查詢的簡(jiǎn)單操作技巧,需要的朋友可以參考下
    2017-06-06
  • 基于Python中的turtle繪畫星星和星空

    基于Python中的turtle繪畫星星和星空

    這篇文章主要介紹了基于Python中的turtle繪畫星星和星空,turtle?是?Python?中自帶的繪圖模塊,下文章關(guān)于turtle繪畫星星和星空的詳細(xì)內(nèi)容,需要的朋友可以參考一下,可以當(dāng)作學(xué)習(xí)小練習(xí)
    2022-03-03
  • nonebot插件之chatgpt使用詳解

    nonebot插件之chatgpt使用詳解

    這篇文章主要為大家介紹了nonebot插件之chatgpt使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Pycharm+Flask零基礎(chǔ)項(xiàng)目搭建入門的實(shí)現(xiàn)

    Pycharm+Flask零基礎(chǔ)項(xiàng)目搭建入門的實(shí)現(xiàn)

    本文主要介紹了Pycharm+Flask零基礎(chǔ)項(xiàng)目搭建入門的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04

最新評(píng)論