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

Pandas讀取行列數(shù)據(jù)最全方法

 更新時間:2021年08月13日 08:39:26   作者:Sunshine168  
本文主要介紹了Pandas讀取行列數(shù)據(jù)最全方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

1、讀取方法有按行(單行,多行連續(xù),多行不連續(xù)),按列(單列,多列連續(xù),多列不連續(xù));部分不連續(xù)行不連續(xù)列;按位置(坐標(biāo)),按字符(索引);按塊(list);函數(shù)有 df.iloc(), df.loc(), df.iat(), df.at(), df.ix()。

2、轉(zhuǎn)換為DF,賦值columns,index,修改添加數(shù)據(jù),取行列索引

data = {'省份': ['北京', '上海', '廣州', '深圳'],
        '年份': ['2017', '2018', '2019', '2020'],
        '總?cè)藬?shù)': ['2200', '1900', '2170', '1890'],
        '高考人數(shù)': ['6.3', '5.9', '6.0', '5.2']}
df = pd.DataFrame(data, columns=['省份', '年份', '總?cè)藬?shù)', '高考人數(shù)', '高數(shù)'],
                  index=['one', 'two', 'three', 'four'])
df['高數(shù)'] = ['90', '95', '92', '98']
print("行索引:{}".format(list(df.index)))
print("列索引:{}".format(list(df.columns)))
print(df.index[1:3])
print(df.columns[1])
print(df.columns[1:3])
print(df)

行索引:['one', 'two', 'three', 'four']
列索引:['省份', '年份', '總?cè)藬?shù)', '高考人數(shù)', '高數(shù)']
Index(['two', 'three'], dtype='object')
年份
Index(['年份', '總?cè)藬?shù)'], dtype='object')
       省份    年份   總?cè)藬?shù) 高考人數(shù)  高數(shù)
one    北京  2017  2200  6.3  90
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
four   深圳  2020  1890  5.2  98

3、iloc不能通過[:, [1:3]]取連續(xù)數(shù)據(jù),取連續(xù)數(shù)據(jù)只能通過 df[df.columns[1:4]],先獲取列索引,再取數(shù)據(jù)。

print(df['省份'])  #按列名取列
print(df.省份)  #按列名取列
print(df[['省份', '總?cè)藬?shù)']])  #按列名取不連續(xù)列數(shù)據(jù)
print(df[df.columns[1:4]])  #按列索引取連續(xù)列數(shù)據(jù)
print(df.iloc[:, 1])  #按位置取列
print(df.iloc[:, [1, 3]])  #按位置取不連續(xù)列數(shù)據(jù)

one      北京
two      上海
three    廣州
four     深圳
Name: 省份, dtype: object
one      北京
two      上海
three    廣州
four     深圳
Name: 省份, dtype: object
       省份   總?cè)藬?shù)
one    北京  2200
two    上海  1900
three  廣州  2170
four   深圳  1890
         年份   總?cè)藬?shù) 高考人數(shù)
one    2017  2200  6.3
two    2018  1900  5.9
three  2019  2170  6.0
four   2020  1890  5.2
one      2017
two      2018
three    2019
four     2020
Name: 年份, dtype: object
         年份 高考人數(shù)
one    2017  6.3
two    2018  5.9
three  2019  6.0
four   2020  5.2

4、通過df.iloc[](數(shù)字)取行數(shù)據(jù),取部分行部分列時,要先寫行,再寫列;有條件的取數(shù)據(jù)

print(df[1:3])  #按行取數(shù)據(jù),這行代碼結(jié)果沒在下面輸出
print(df[df.高數(shù)>90])  #按行有條件的取數(shù)據(jù),結(jié)果沒輸出
print(df.iloc[1])  #按行取行數(shù)據(jù)
print(df.iloc[1, 3])  #按坐標(biāo)取
print(df.iloc[[1], [3]])  #按坐標(biāo)取
print(df.loc[df.index[1:3]])  #按行索引取行,但沒必要
print(df.iloc[1:3])  #按行取連續(xù)數(shù)據(jù)
print(df.iloc[[1, 3]])  按行取不連續(xù)數(shù)據(jù)
print(df.iloc[[1,2,3], [2,4]])  取部分行部分列數(shù)據(jù)

省份        上海
年份      2018
總?cè)藬?shù)     1900
高考人數(shù)     5.9
高數(shù)        95
Name: two, dtype: object
5.9
    高考人數(shù)
two  5.9
       省份    年份   總?cè)藬?shù) 高考人數(shù)  高數(shù)
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
       省份    年份   總?cè)藬?shù) 高考人數(shù)  高數(shù)
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
      省份    年份   總?cè)藬?shù) 高考人數(shù)  高數(shù)
two   上海  2018  1900  5.9  95
four  深圳  2020  1890  5.2  98
        總?cè)藬?shù)  高數(shù)
two    1900  95
three  2170  92
four   1890  98

5、通過df.loc[]索引(字符)取行數(shù)據(jù)。

print(df.loc['two'])
print(df.loc['two', '省份'])
print(df.loc['two':'three'])
print(df.loc[['one', 'three']])
print(df.loc[['one', 'three'], ['省份', '年份']])

省份        上海
年份      2018
總?cè)藬?shù)     1900
高考人數(shù)     5.9
高數(shù)        95
Name: two, dtype: object
上海
       省份    年份   總?cè)藬?shù) 高考人數(shù)  高數(shù)
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
       省份    年份   總?cè)藬?shù) 高考人數(shù)  高數(shù)
one    北京  2017  2200  6.3  90
three  廣州  2019  2170  6.0  92
       省份    年份
one    北京  2017
three  廣州  2019

6、ix,iat,at取行列數(shù)據(jù),此方法不常用,可以使用上面方法即可。

print(df.ix[1:3])
print(df.ix[:, [1, 3]])
print(df.iat[1,3])
print(df.at['two', '省份'])

       省份    年份   總?cè)藬?shù) 高考人數(shù)  高數(shù)
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
         年份 高考人數(shù)
one    2017  6.3
two    2018  5.9
three  2019  6.0
four   2020  5.2
5.9
上海

到此這篇關(guān)于Pandas讀取行列數(shù)據(jù)最全方法的文章就介紹到這了,更多相關(guān)Pandas讀取行列 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • windows下python之mysqldb模塊安裝方法

    windows下python之mysqldb模塊安裝方法

    這篇文章主要介紹了windows下python之mysqldb模塊安裝方法,需要的朋友可以參考下
    2017-09-09
  • pygame游戲之旅 添加游戲界面按鍵圖形

    pygame游戲之旅 添加游戲界面按鍵圖形

    這篇文章主要為大家詳細(xì)介紹了pygame游戲之旅的第10篇,教大家如何添加游戲界面按鍵圖形,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Python Tkinter GUI編程實現(xiàn)Frame切換

    Python Tkinter GUI編程實現(xiàn)Frame切換

    本文主要介紹了Python Tkinter GUI編程實現(xiàn)Frame切換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • python3.6的字符串處理f-string的使用技巧分享

    python3.6的字符串處理f-string的使用技巧分享

    在這篇文章中講解了F字符串的基礎(chǔ)使用,對于F字符串有著很多的使用技巧,在這篇文章中你會見識到更多的F字符串的使用技巧,下面小編將介紹python3.6?的字符串處理f-string的使用技巧,需要的朋友可以參考下
    2024-02-02
  • Python Tkinter Canvas畫布控件詳解

    Python Tkinter Canvas畫布控件詳解

    Canvas 控件具有兩個功能,首先它可以用來繪制各種圖形,比如弧形、線條、橢圓形、多邊形和矩形等,其次 Canvas 控件還可以用來展示圖片。本文將具體介紹一直Tkinter中的畫布控件,需要的可以參考一下
    2022-01-01
  • 對python PLT中的image和skimage處理圖片方法詳解

    對python PLT中的image和skimage處理圖片方法詳解

    今天小編就為大家分享一篇對python PLT中的image和skimage處理圖片方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python中文件路徑的拼接的幾種方法實例

    Python中文件路徑的拼接的幾種方法實例

    在Python開發(fā)中,文件路徑的拼接是一個常見而且重要的任務(wù),正確的路徑拼接可以確保程序在不同平臺上都能正常運行,本文將介紹Python中文件路徑拼接的幾種方式,包括使用os.path.join、os.path.join、pathlib庫以及os.path.join
    2023-12-12
  • Python 一篇文章看懂時間日期對象

    Python 一篇文章看懂時間日期對象

    學(xué)習(xí)每一門語言多多少少會用到與時間相關(guān)的東西,咱們今天呢就談一談Python中的時間與日期對象。在Python中時間對象一般可以用來測量效率。日期對象用來處理日期與字符串之間的關(guān)系
    2022-03-03
  • python模塊中pip命令的基本使用

    python模塊中pip命令的基本使用

    這篇文章主要為大家介紹了python機器學(xué)習(xí)python實現(xiàn)神經(jīng)網(wǎng)絡(luò)的示例解析,在同樣在進行python機器學(xué)習(xí)的同學(xué)可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • python3?http.client?網(wǎng)絡(luò)請求方式

    python3?http.client?網(wǎng)絡(luò)請求方式

    這篇文章主要介紹了python3?http.client?網(wǎng)絡(luò)請求方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評論