網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、含正負(fù)號(hào)的下標(biāo)
正下標(biāo)從0開(kāi)始,負(fù)下標(biāo)從-1開(kāi)始1。切片的時(shí)候包括頭不包括尾部。
二、loc和iloc
loc是指location的意思,iloc中的i是指integer。
【1】iloc:根據(jù)標(biāo)簽的所在位置,從0開(kāi)始計(jì)數(shù),先選取行再選取列
【2】loc:根據(jù)DataFrame的具體標(biāo)簽選取行列,同樣是先行標(biāo)簽,后列標(biāo)簽
由上圖可以看出:iloc[:4,2]和loc[:4,2]是不一樣的,前者不包括4,后者包括4
lypdfdata=lypdf.iloc[:,1:-1].values
lypdftarget=lypdf.iloc[:,:-1].values
# 逗號(hào)前面是屬于行,后面是屬于列
1. 利用loc、iloc提取行數(shù)據(jù)
import numpy as np
import pandas as pd
#創(chuàng)建一個(gè)Dataframe
data=pd.DataFrame(np.arange(16).reshape(4,4),index=list('abcd'),columns=list('ABCD'))
In[1]: data
Out[1]:
A B C D
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
#取索引為'a'的行
In[2]: data.loc['a']
Out[2]:
A 0
B 1
C 2
D 3
#取第一行數(shù)據(jù),索引為'a'的行就是第一行,所以結(jié)果相同
In[3]: data.iloc[0]
Out[3]:
A 0
B 1
C 2
D 3
2. 利用loc、iloc提取列數(shù)據(jù)
In[4]:data.loc[:,['A']] #取'A'列所有行,多取幾列格式為 data.loc[:,['A','B']]
Out[4]:
A
a 0
b 4
c 8
d 12
In[5]:data.iloc[:,[0]] #取第0列所有行,多取幾列格式為 data.iloc[:,[0,1]]
Out[5]:
A
a 0
b 4
c 8
d 12
3.利用loc、iloc提取指定行、指定列數(shù)據(jù)
In[6]:data.loc[['a','b'],['A','B']] #提取index為'a','b',列名為'A','B'中的數(shù)據(jù)
Out[6]:
A B
a 0 1
b 4 5
In[7]:data.iloc[[0,1],[0,1]] #提取第0、1行,第0、1列中的數(shù)據(jù)
Out[7]:
A B
a 0 1
b 4 5
4.利用loc、iloc提取所有數(shù)據(jù)
In[8]:data.loc[:,:] #取A,B,C,D列的所有行
Out[8]:
A B C D
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
In[9]:data.iloc[:,:] #取第0,1,2,3列的所有行
Out[9]:
A B C D
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
5.利用loc函數(shù),根據(jù)某個(gè)數(shù)據(jù)來(lái)提取數(shù)據(jù)所在的行
In[10]: data.loc[data['A']==0] #提取data數(shù)據(jù)(篩選條件: A列中數(shù)字為0所在的行數(shù)據(jù))
Out[10]:
A B C D
a 0 1 2 3
In[11]: data.loc[(data['A']==0)&(data['B']==2)] #提取data數(shù)據(jù)(多個(gè)篩選條件)
Out[11]:
A B C D
a 0 1 2 3
原文鏈接:https://blog.csdn.net/wxfighting/article/details/124916726
相關(guān)推薦
- 2022-09-03 Python?pandas?DataFrame數(shù)據(jù)拼接方法_python
- 2022-09-22 git commit后,如何撤銷(xiāo)commit
- 2022-05-12 小程序滾動(dòng)穿透解決方案
- 2021-12-16 方案缺陷-HAProxy + Sentinel +redis
- 2022-07-14 如何修改numpy?array的數(shù)據(jù)類(lèi)型_python
- 2022-05-08 繼docker之后podman容器技術(shù)崛起_docker
- 2022-12-26 C++逆向分析移除鏈表元素實(shí)現(xiàn)方法詳解_C 語(yǔ)言
- 2022-08-12 Python?Pandas?中的數(shù)據(jù)結(jié)構(gòu)詳解_python
- 最近更新
-
- 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概述快速入門(mén)
- 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)程分支