網(wǎng)站首頁 編程語言 正文
DataFrame的行和列:df[‘行’, ‘列’]
DataFrame行和列的獲取分三個維度
- 行和列選取:df[],一次只能選取行或列
- 區(qū)域選取:df.loc[], df.iloc[], df.ix[],可以同時為行或列設(shè)置篩選條件
- 單元格選取:df.at[], df.iat[],準(zhǔn)確選取某個單元格
先隨機生成一個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ù)組長度需等于行數(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)行篩選時,前閉后閉。
- df.iloc[] 只能使用整數(shù)索引,不能使用標(biāo)簽索引,通過整數(shù)索引切邊進(jìn)行篩選時,前閉后開。
- df.ix[]既可以使用標(biāo)簽索引,也可以使用整數(shù)索引。
2.1 df.loc[]
pandas.DataFrame.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 同時選取行和列
# 選取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 ? ? ?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[ ]
pandas.DataFrame.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[ ]
可以混合標(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[]
pandas.DataFrame.at 官方文檔
獲取c行C列的值
df.at['c', 'C']
把c行C列賦值為10
df.at['c', 'C'] = 10
3.2 df.iat[]
pandas.DataFrame.iat 官方文檔
獲取第三行第三列的值
df.iat[2, 2]
把第三行第三列賦值為10
df.iat[2, 2] = 10
原文鏈接:https://blog.csdn.net/weixin_46599926/article/details/122795057
相關(guān)推薦
- 2023-07-07 Tomcat文件夾屬性
- 2022-06-13 matplotlib繪制甘特圖的萬能模板案例_python
- 2022-09-04 Go語言中的函數(shù)詳解_Golang
- 2022-04-05 C語言實現(xiàn)自動售貨機_C 語言
- 2022-07-13 Andorid 自定義 View - 自定義屬性 - 屬性重復(fù)導(dǎo)致沖突
- 2022-07-30 react擴展6_renderProps
- 2023-11-18 python字符串string的截取;獲取字符串內(nèi)的一串
- 2022-12-08 C語言中-a++和-++a運算順序?qū)嵗馕鯻C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支