pandas行和列的獲取的實(shí)現(xiàn)
DataFrame的行和列:df[‘行’, ‘列’]
DataFrame行和列的獲取分三個(gè)維度
- 行和列選?。篸f[],一次只能選取行或列
- 區(qū)域選?。篸f.loc[], df.iloc[], df.ix[],可以同時(shí)為行或列設(shè)置篩選條件
- 單元格選?。篸f.at[], df.iat[],準(zhǔn)確選取某個(gè)單元格
先隨機(jī)生成一個(gè)dataframe
import numpy as np import pandas as pd df = pd.DataFrame(np.random.randn(10,5), index=list('abcdefghij'), columns=list('ABCDE')) # Output ?? ? ? A?? ? ? ? ? ?B?? ? ? ? ? C?? ? ? ? ? ?D?? ? ? ? ? ?E a?? ?0.299206?? ?-0.383297?? ?-0.931467?? ?-0.591609?? ?-1.131105 b?? ?0.074351?? ?0.791849?? ?1.637467?? ?-1.408712?? ?-1.376527 c?? ?-0.359802?? ?-2.049489?? ?-0.615742?? ?-1.953994?? ?0.685243 d?? ?0.232557?? ?1.768284?? ?-0.447015?? ?2.373358?? ?1.220536 e?? ?-0.997380?? ?-0.447236?? ?0.632368?? ?-0.352590?? ?-0.064736 f?? ?-1.220178?? ?-0.314304?? ?1.202184?? ?0.018326?? ?1.072153 g?? ?-1.508916?? ?0.380466?? ?0.359506?? ?-0.742657?? ?-0.373764 h?? ?1.031420?? ?-3.236676?? ?0.444769?? ?1.396802?? ?-0.405590 i?? ?0.166133?? ?-0.051614?? ?-0.146943?? ?0.609431?? ?-0.351814 j?? ?1.857521?? ?-0.159101?? ?0.899745?? ?1.108722?? ?-0.615379
1. 行和列的獲取
1.1 根據(jù)索引獲取行
獲取前3行數(shù)據(jù)
df[:3] # Output A B C D E a 0.299206 -0.383297 -0.931467 -0.591609 -1.131105 b 0.074351 0.791849 1.637467 -1.408712 -1.376527 c -0.359802 -2.049489 -0.615742 -1.953994 0.685243
獲取第2到3行數(shù)據(jù)
df[1:3] # 前閉后開 df['b':'c'] # # 前閉后閉 # Output A B C D E b 0.074351 0.791849 1.637467 -1.408712 -1.376527 c -0.359802 -2.049489 -0.615742 -1.953994 0.685243
獲取特定行數(shù)據(jù)
# 布爾數(shù)組 (數(shù)組長(zhǎng)度需等于行數(shù)) df[[True,False,True,False,False,False, True, True, False, True]] # Output A B C D E a 0.299206 -0.383297 -0.931467 -0.591609 -1.131105 c -0.359802 -2.049489 -0.615742 -1.953994 0.685243 g -1.508916 0.380466 0.359506 -0.742657 -0.373764 h 1.031420 -3.236676 0.444769 1.396802 -0.405590 j 1.857521 -0.159101 0.899745 1.108722 -0.615379
1.2 根據(jù)條件獲取行
獲取A列大于0的行
df[df.A > 0] # Output A B C D E a 0.299206 -0.383297 -0.931467 -0.591609 -1.131105 b 0.074351 0.791849 1.637467 -1.408712 -1.376527 d 0.232557 1.768284 -0.447015 2.373358 1.220536 h 1.031420 -3.236676 0.444769 1.396802 -0.405590 i 0.166133 -0.051614 -0.146943 0.609431 -0.351814 j 1.857521 -0.159101 0.899745 1.108722 -0.615379
獲取A列和B列大于0的行
df[(df.A > 0) & (df.B > 0)] # Output A B C D E b 0.074351 0.791849 1.637467 -1.408712 -1.376527 d 0.232557 1.768284 -0.447015 2.373358 1.220536
獲取A列或列大于0的行
df[(df.A > 0) | (df.B > 0)] # Output A B C D E a 0.299206 -0.383297 -0.931467 -0.591609 -1.131105 b 0.074351 0.791849 1.637467 -1.408712 -1.376527 d 0.232557 1.768284 -0.447015 2.373358 1.220536 g -1.508916 0.380466 0.359506 -0.742657 -0.373764 h 1.031420 -3.236676 0.444769 1.396802 -0.405590 i 0.166133 -0.051614 -0.146943 0.609431 -0.351814 j 1.857521 -0.159101 0.899745 1.108722 -0.615379
1.3 獲取列
# 獲取A列 df['A'] # 輸出為Series類型 df[['A']] # 輸出為DataFrame類型 # 獲取A列和B列 df[['A', 'B']] df[df.columns[0:2]]
2. 區(qū)域選取
- df.loc[] 只能使用標(biāo)簽索引,不能使用整數(shù)索引,通過便簽索引切邊進(jìn)行篩選時(shí),前閉后閉。
- df.iloc[] 只能使用整數(shù)索引,不能使用標(biāo)簽索引,通過整數(shù)索引切邊進(jìn)行篩選時(shí),前閉后開。
- df.ix[]既可以使用標(biāo)簽索引,也可以使用整數(shù)索引。
2.1 df.loc[]
2.1.1 行選取
獲取a行
# 輸出為Series類型 df.loc['a'] df.loc['a', :] # Output A ? ?0.299206 B ? -0.383297 C ? -0.931467 D ? -0.591609 E ? -1.131105 Name: a, dtype: float64 # 輸出為DataFrame類型 df.loc[['a']] df.loc[['a'], :] # Output ?? ? ? A?? ? ? ? ? ?B?? ? ? ? ? C?? ? ? ? ? ?D?? ? ? ? ? ?E a?? ?0.299206?? ?-0.383297?? ?-0.931467?? ?-0.591609?? ?-1.131105
獲取a, b, d行
# 使用標(biāo)簽索引 df.loc[['a', 'b', 'd']] df.loc[['a', 'b', 'd'], :] # 使用布爾數(shù)組 df[[True, True, False, True, False, False, False, True, False, True]] # Output A B C D E a 0.299206 -0.383297 -0.931467 -0.591609 -1.131105 b 0.074351 0.791849 1.637467 -1.408712 -1.376527 d 0.232557 1.768284 -0.447015 2.373358 1.220536
獲取a到d行
df.loc['a':'d', :] # Output A B C D E a 0.299206 -0.383297 -0.931467 -0.591609 -1.131105 b 0.074351 0.791849 1.637467 -1.408712 -1.376527 c -0.359802 -2.049489 -0.615742 -1.953994 0.685243 d 0.232557 1.768284 -0.447015 2.373358 1.220536
選取A列大于0的行
df.loc[df.A > 0] df.loc[df.A > 0, :] # Output A B C D E a 0.299206 -0.383297 -0.931467 -0.591609 -1.131105 b 0.074351 0.791849 1.637467 -1.408712 -1.376527 d 0.232557 1.768284 -0.447015 2.373358 1.220536 h 1.031420 -3.236676 0.444769 1.396802 -0.405590 i 0.166133 -0.051614 -0.146943 0.609431 -0.351814 j 1.857521 -0.159101 0.899745 1.108722 -0.615379
2.1.2 列選取
# 選取A列 df.loc[:, 'A'] # 選取A列和C列 df.loc[:, ['A', 'C']] # 選取A列到C列 df.loc[:, 'A':'C']
2.1.3 同時(shí)選取行和列
# 選取c行B列的值 df.loc['c', 'B'] # 選取A列和B列同時(shí)大于0的C列和D列 df.loc[((df.A > 0) & (df.B > 0)), ['C', 'D']]
2.1.4 行和列的賦值
# 令a行為10 df.loc['a', :] = 10 # 令B列為50 df.loc[:, 'B'] = 50 # 令b, c行的C到F列為30 df.loc[['b', 'c'], 'C':'F'] = 30 # 令C列小于0的行賦值為0 df.loc[df.C < 0] = 0
2.1.5 多重索引
Example
tuples = [ ? ?('cobra', 'mark i'), ('cobra', 'mark ii'), ? ?('sidewinder', 'mark i'), ('sidewinder', 'mark ii'), ? ?('viper', 'mark ii'), ('viper', 'mark iii') ] index = pd.MultiIndex.from_tuples(tuples) values = [[12, 2], [0, 4], [10, 20], ? ? ? ? [1, 4], [7, 1], [16, 36]] df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index) # Output df ? ? ? ? ? ? ? ? ? ? ?max_speed ?shield cobra ? ? ?mark i ? ? ? ? ? 12 ? ? ? 2 ? ? ? ? ? ?mark ii ? ? ? ? ? 0 ? ? ? 4 sidewinder mark i ? ? ? ? ? 10 ? ? ?20 ? ? ? ? ? ?mark ii ? ? ? ? ? 1 ? ? ? 4 viper ? ? ?mark ii ? ? ? ? ? 7 ? ? ? 1 ? ? ? ? ? ?mark iii ? ? ? ? 16 ? ? ?36
df.loc['cobra'] max_speed shield mark i 12 2 mark ii 0 4
# return a Series df.loc[('cobra', 'mark ii')]? max_speed ? ?0 shield ? ? ? 4 Name: (cobra, mark ii), dtype: int64 # return a dataframe df.loc[[('cobra', 'mark ii')]] ? ? ? ? ? ? ? ?max_speed ?shield cobra mark ii ? ? ? ? ?0 ? ? ? 4
# return a Series df.loc['cobra', 'mark i'] max_speed 12 shield 2 Name: (cobra, mark i), dtype: int64
df.loc[('cobra', 'mark i'), 'shield']
df.loc[('cobra', 'mark i'):'viper'] ? ? ? ? ? ? ? ? ? ? ?max_speed ?shield cobra ? ? ?mark i ? ? ? ? ? 12 ? ? ? 2 ? ? ? ? ? ?mark ii ? ? ? ? ? 0 ? ? ? 4 sidewinder mark i ? ? ? ? ? 10 ? ? ?20 ? ? ? ? ? ?mark ii ? ? ? ? ? 1 ? ? ? 4 viper ? ? ?mark ii ? ? ? ? ? 7 ? ? ? 1 ? ? ? ? ? ?mark iii ? ? ? ? 16 ? ? ?36 df.loc[('cobra', 'mark i'):('viper', 'mark ii')] ? ? ? ? ? ? ? ? ? ? max_speed ?shield cobra ? ? ?mark i ? ? ? ? ?12 ? ? ? 2 ? ? ? ? ? ?mark ii ? ? ? ? ?0 ? ? ? 4 sidewinder mark i ? ? ? ? ?10 ? ? ?20 ? ? ? ? ? ?mark ii ? ? ? ? ?1 ? ? ? 4 viper ? ? ?mark ii ? ? ? ? ?7 ? ? ? 1
2.2 df.iloc[ ]
2.2.1 行選取
選取第二行
# return a Series df.iloc[1] df.iloc[1, :] # return a dataframe df.iloc[[1]] df.iloc[[1], :]
選取前三行
df.iloc[:3, :] df.iloc[:3]
選取第一、三、五行
df.iloc[[1, 3, 5]] df.iloc[[1, 3, 5], :]
2.2.2 列選取
選取第二列
df.iloc[:, 1]
選取前三列
df.iloc[:, 0:3] df.iloc[:,:3]
選取第一三四列
df.iloc[:, [0, 2, 3]]
2.2.3 同時(shí)選取行和列
選取第一行第二列的值
df.iloc[0, 1]
選取第二三行的第二到四列
df.iloc[[1,2], 1:4]
2.3 df.ix[ ]
可以混合標(biāo)簽索引和整數(shù)索引
However, when an axis is integer based, ONLY label based access and not positional access is supported. Thus, in such cases, it’s usually better to be explicit and use .iloc or .loc.
3. 單元格選取
- df.at[ ] 只能使用標(biāo)簽索引
- df.iat[ ] 只能使用整數(shù)索引
3.1 df.at[]
獲取c行C列的值
df.at['c', 'C']
把c行C列賦值為10
df.at['c', 'C'] = 10
3.2 df.iat[]
獲取第三行第三列的值
df.iat[2, 2]
把第三行第三列賦值為10
df.iat[2, 2] = 10
到此這篇關(guān)于pandas行和列的獲取的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)pandas行列獲取內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基礎(chǔ)的十進(jìn)制按位運(yùn)算總結(jié)與在Python中的計(jì)算示例
按位運(yùn)算是計(jì)算機(jī)學(xué)習(xí)中的基礎(chǔ),Python完全支持位運(yùn)算符從而幾乎可以直接顯示出位運(yùn)算的結(jié)果,這里我們稍微總結(jié)一下基礎(chǔ)的十進(jìn)制按位運(yùn)算總結(jié)與在Python中的計(jì)算示例2016-06-06python3利用smtplib通過qq郵箱發(fā)送郵件方法示例
python實(shí)現(xiàn)郵件發(fā)送較為簡(jiǎn)單,主要用到smtplib這個(gè)模塊,所以下面這篇文章主要給大家介紹了關(guān)于python3利用smtplib通過qq郵箱發(fā)送郵件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起看看吧。2017-12-12pygame播放視頻并實(shí)現(xiàn)音視頻同步的解決方案
本篇提供了pygame播放視頻的兩個(gè)方案,第一個(gè)方案是網(wǎng)上找的現(xiàn)成的,第二個(gè)方案則是參考了opencv(主流方案)自己逆向思維做的,還未經(jīng)過實(shí)際驗(yàn)證,感興趣的朋友跟隨小編一起看看吧2023-11-11Python通過TensorFLow進(jìn)行線性模型訓(xùn)練原理與實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Python通過TensorFLow進(jìn)行線性模型訓(xùn)練原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Python通過TensorFLow進(jìn)行線性模型訓(xùn)練相關(guān)概念、算法設(shè)計(jì)與訓(xùn)練操作技巧,需要的朋友可以參考下2020-01-01用Python進(jìn)行屏幕錄制的實(shí)現(xiàn)
關(guān)于屏幕錄制這個(gè)功能需求,之前用過基于ffmpeg的Capture錄屏軟件,但是fps拉高以后會(huì)變得很卡,聲音也同樣出現(xiàn)卡頓,所以本文給大家介紹了用Python進(jìn)行屏幕錄制的實(shí)現(xiàn),感興趣的朋友可以參考下2024-04-04Python實(shí)現(xiàn)將doc轉(zhuǎn)化pdf格式文檔的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將doc轉(zhuǎn)化pdf格式文檔的方法,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)doc格式文件讀取及轉(zhuǎn)換pdf格式文件的操作技巧,以及php調(diào)用py文件的具體實(shí)現(xiàn)方法,需要的朋友可以參考下2018-01-01