網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1.索引是什么
1.1 認(rèn)識(shí)索引
先創(chuàng)建一個(gè)簡(jiǎn)單的DataFrame。
myList = [['a', 10, 1.1],
['b', 20, 2.2],
['c', 30, 3.3],
['d', 40, 4.4]]
df1 = pd.DataFrame(data = myList)
print(df1)
--------------------------------
[out]:
0 1 2
0 a 10 1.1
1 b 20 2.2
2 c 30 3.3
3 d 40 4.4
DataFrame中有兩種索引:
- 行索引(index):對(duì)應(yīng)最左邊那一豎列
- 列索引(columns):對(duì)應(yīng)最上面那一橫行
兩種索引默認(rèn)均為從0開始的自增整數(shù)。
# 輸出行索引
print(df1.index)
[out]:
RangeIndex(start=0, stop=4, step=1)
---------------------------------------
# 輸出列索引
print(df1.columns)
[out]:
RangeIndex(start=0, stop=3, step=1)
---------------------------------------
# 輸出所有的值
print(df1.values)
[out]:
array([['a', 10, 1.1],
['b', 20, 2.2],
['c', 30, 3.3],
['d', 40, 4.4]], dtype=object)
1.2 自定義索引
可以使用 index 這個(gè)參數(shù)指定行索引,columns 這個(gè)參數(shù)指定列索引。
df2 = pd.DataFrame(myList,
index = ['one', 'two', 'three', 'four'],
columns = ['char', 'int', 'float'])
print(df2)
-----------------------------------------------------------
[out]:
char int float
one a 10 1.1
two b 20 2.2
three c 30 3.3
four d 40 4.4
輸出此時(shí)的行索引和列索引:
# 輸出行索引
print(df2.index)
[out]:
Index(['one', 'two', 'three', 'four'], dtype='object')
--------------------------------------------------------
# 輸出列索引
print(df2.columns)
[out]:
Index(['char', 'int', 'float'], dtype='object')
2. 索引的簡(jiǎn)單使用
2.1 列索引
選擇一列:
print(df2['char'])
print(df2.char)
# 兩種方式輸出一樣
[out]:
one a
two b
three c
four d
Name: char, dtype: object
注意此時(shí)方括號(hào)里面只傳入一個(gè)字符串’char’,這樣選出來(lái)的一列,結(jié)果的類型為Series
print(df2['char'])
print(df2.char)
# 兩種方式輸出一樣
[out]:
one a
two b
three c
four d
Name: char, dtype: object
選擇多列:
print(df2[['char', 'int']])
[out]:
char int
one a 10
two b 20
three c 30
four d 40
注意此時(shí)方括號(hào)里面?zhèn)魅胍粋€(gè)列表 [‘char’, ‘int’],選出的結(jié)果類型為 DataFrame。
如果只想選出來(lái)一列,卻想返回 DataFrame 類型怎么辦?
print(df2[['char']])
[out]:
char
one a
two b
three c
four d
---------------------------------------
type(df2[['char']])
[out]:pandas.core.frame.DataFrame
注意直接使用df2[0]取某一列會(huì)報(bào)錯(cuò),除非columns是由下標(biāo)索引組成的,比如df1那個(gè)樣子,df1[0]就不會(huì)報(bào)錯(cuò)。
print(df1[0])
[out]:
0 a
1 b
2 c
3 d
Name: 0, dtype: object
-----------------------
print(df2[0])
[out]:
KeyError: 0
2.2 行索引
2.2.1 使用[ ]
區(qū)別于選取列,此種方式[ ]中不再單獨(dú)的傳入一個(gè)字符串,而是需要使用冒號(hào)切片。
選取行標(biāo)簽從 ’two’ 到 ’three’ 的多行數(shù)據(jù)
print(df2['two': 'three'])
[out]:
char int float
two b 20 2.2
three c 30 3.3
選取行標(biāo)簽為’two’這一行數(shù)據(jù)
# 此時(shí)返回的類型為DataFrame
print(df2['two': 'two'])
[out]:
char int float
two b 20 2.2
在[ ]中不僅可以傳入行標(biāo)簽,還可以傳入行的編號(hào)。
選取從第1行到第3行的數(shù)據(jù)(編號(hào)從0開始)
print(df2[1:4])
[out]:
char int float
two b 20 2.2
three c 30 3.3
four d 40 4.4
可以看到選取的數(shù)據(jù)是不包含方括號(hào)最右側(cè)的編號(hào)所對(duì)應(yīng)的數(shù)據(jù)的。
選取第1行的數(shù)據(jù)
print(df2[1:2])
[out]:
char int float
two b 20 2.2
2.2.2 使用.loc()和.iloc()
區(qū)別就是.loc()是根據(jù)行索引和列索引的值來(lái)選取數(shù)據(jù),而.iloc()是根據(jù)從0開始的下標(biāo)位置來(lái)進(jìn)行索引的。
選取行:
使用.loc()
print(df2.loc['one'])
[out]:
char a
int 10
float 1.1
Name: one, dtype: object
-------------------------------------------
print(df2.loc[['one', 'three']])
[out]:
char int float
one a 10 1.1
three c 30 3.3
使用.iloc()
print(df2.iloc[0])
[out]:
char a
int 10
float 1.1
Name: one, dtype: object
-------------------------------------------
print(df2.iloc[[0, 2]])
[out]:
char int float
one a 10 1.1
three c 30 3.3
原文鏈接:https://blog.csdn.net/weixin_46713695/article/details/125959391
相關(guān)推薦
- 2021-12-06 CentOS環(huán)境使用NFS遠(yuǎn)程目錄掛載過(guò)程介紹_Linux
- 2022-04-05 less calc計(jì)算有問(wèn)題
- 2022-10-09 django中的自定義分頁(yè)器的實(shí)現(xiàn)示例_python
- 2022-09-30 Python3中map()、reduce()、filter()的用法詳解_python
- 2022-05-04 分享10提高?Python?代碼的可讀性的技巧_python
- 2023-02-27 pandas?pd.cut()與pd.qcut()的具體實(shí)現(xiàn)_python
- 2022-09-04 python實(shí)現(xiàn)自動(dòng)生成C++代碼的代碼生成器_python
- 2021-12-13 Go中時(shí)間與時(shí)區(qū)問(wèn)題的深入講解_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支