pandas行和列的獲取的實現(xiàn)
DataFrame的行和列:df[‘行’, ‘列’]
DataFrame行和列的獲取分三個維度
- 行和列選?。篸f[],一次只能選取行或列
- 區(qū)域選?。篸f.loc[], df.iloc[], df.ix[],可以同時為行或列設(shè)置篩選條件
- 單元格選?。篸f.at[], df.iat[],準確選取某個單元格
先隨機生成一個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.6153791. 行和列的獲取
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ù)組長度需等于行數(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[] 只能使用標簽索引,不能使用整數(shù)索引,通過便簽索引切邊進行篩選時,前閉后閉。
- df.iloc[] 只能使用整數(shù)索引,不能使用標簽索引,通過整數(shù)索引切邊進行篩選時,前閉后開。
- df.ix[]既可以使用標簽索引,也可以使用整數(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行
# 使用標簽索引
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 同時選取行和列
# 選取c行B列的值 df.loc['c', 'B'] # 選取A列和B列同時大于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 ? ? ?36df.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 ? ? ? 12.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 同時選取行和列
選取第一行第二列的值
df.iloc[0, 1]
選取第二三行的第二到四列
df.iloc[[1,2], 1:4]
2.3 df.ix[ ]
可以混合標簽索引和整數(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[ ] 只能使用標簽索引
- 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行和列的獲取的實現(xiàn)的文章就介紹到這了,更多相關(guān)pandas行列獲取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
tensorflow中的數(shù)據(jù)類型dtype用法說明
這篇文章主要介紹了tensorflow中的數(shù)據(jù)類型dtype用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
利用Python實現(xiàn)自動生成小學(xué)生計算題
過年期間發(fā)現(xiàn)小外甥已經(jīng)上小學(xué)了,我姐說老師今天給他們布置了寒假作業(yè):每天堅持做乘法和加減法混合運算。這我必須幫幫忙,用Python寫了一段自動生成小學(xué)生計算題的代碼,希望外甥不要太感謝我2023-02-02
OpenCV圖像卷積之cv.filter2D()函數(shù)詳解
在其官方文檔中,filter2D()函數(shù)在掩模板介紹中一筆帶過,我認為該函數(shù)應(yīng)該進行詳細介紹,下面這篇文章主要給大家介紹了關(guān)于OpenCV圖像卷積之cv.filter2D()函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-09-09
使用Python進行數(shù)據(jù)清洗與存儲的基本方法
在爬蟲數(shù)據(jù)獲取完成后,數(shù)據(jù)往往是“原始”的,不適合直接使用,清洗和存儲是將爬取到的原始數(shù)據(jù)轉(zhuǎn)化為有用信息的關(guān)鍵步驟,本文將系統(tǒng)地介紹 Python 中進行數(shù)據(jù)清洗與存儲的基本方法,幫助新手理解如何處理爬蟲數(shù)據(jù),使其更加適合分析和使用,需要的朋友可以參考下2024-11-11
Python+Django+MySQL實現(xiàn)基于Web版的增刪改查的示例代碼
這篇文章主要介紹了Python+Django+MySQL實現(xiàn)基于Web版的增刪改查的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-05-05

