網站首頁 編程語言 正文
1 loc和iloc的含義
loc表示location的意思;iloc中的loc意思相同,前面的i表示integer,所以它只接受整數作為參數。
2 用法
import pandas as pd
import numpy as np
# np.random.randn(5, 2)表示返回5x2的矩陣,index表示行的編號,columns表示列的編號
df = pd.DataFrame(np.random.randn(5, 2), index=range(0, 5, 1), columns=list('AB'))
print(df)
打印df的結果:
2.1 loc函數的用法
loc表示通過標簽取數據,標簽就是上面的‘0’-‘4’和‘A’-‘B’。
print(df.loc[0])
print(df.loc[0, :])
print(df.loc[0:2, 'A'])
2.2 iloc函數的用法
iloc函數表示通過位置取數據,即第m行,第n列數據,只接受整型參數。記?。?strong>0:2為“包左不包右”,即取0, 1。
print(df.iloc[0, :])
print(df.iloc[:, 0])
print(df.iloc[0:2, :])
補充:Pandas中loc和iloc函數實例
利用loc、iloc提取行數據
import numpy as np
import pandas as pd
#創建一個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
#取第一行數據,索引為'a'的行就是第一行,所以結果相同
In[3]: data.iloc[0]
Out[3]:
A 0
B 1
C 2
D 3
loc函數:通過行索引 “Index” 中的具體值來取行數據(如取"Index"為"A"的行)
iloc函數:通過行號來取行數據(如取第二行的數據)
利用loc、iloc提取列數據
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
總結
原文鏈接:https://blog.csdn.net/weixin_44072535/article/details/112208171
相關推薦
- 2022-01-17 EF關于報錯Self referencing loop detected with type的原因以
- 2022-01-09 出現Got permission denied while trying to connect to
- 2022-10-31 C#實現HTTP訪問類HttpHelper的示例詳解_C#教程
- 2022-04-20 .NET?6新特性試用Timer類之PeriodicTimer?_ASP.NET
- 2022-10-05 Python中matplotlib庫安裝失敗的經驗總結(附pycharm配置anaconda)_py
- 2022-06-29 Tomcat配置訪問日志和線程數的實現步驟_Tomcat
- 2022-05-29 MVC使用MvcPager實現分頁效果_實用技巧
- 2022-07-12 k8s 之 service ip
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支