網站首頁 編程語言 正文
對于 Pandas 根據條件獲取指定數據,相信大家都能夠輕松的寫出相應代碼,但是如果你還沒用過 query,相信你會被它的簡潔所折服!
常規用法
先創建一個 DataFrame。
import pandas as pd df = pd.DataFrame( ? ? {'A': ['e', 'd', 'c', 'b', 'a'], ? ? ?'B': ['f', 'b', 'c', 'd', 'e'], ? ? ?'C': range(0, 10, 2), ? ? ?'D': range(10, 0, -2), ? ? ?'E.E': range(10, 5, -1)})
我們現在選取 A列字母出現在B列 的所有行。先看兩種常見寫法。
>>> df[df['A'].isin(df['B'])] ? ?A ?B ?C ? D ?E.E 0 ?e ?f ?0 ?10 ? 10 1 ?d ?b ?2 ? 8 ? ?9 2 ?c ?c ?4 ? 6 ? ?8 3 ?b ?d ?6 ? 4 ? ?7 >>> df.loc[df['A'].isin(df['B'])] ? ?A ?B ?C ? D ?E.E 0 ?e ?f ?0 ?10 ? 10 1 ?d ?b ?2 ? 8 ? ?9 2 ?c ?c ?4 ? 6 ? ?8 3 ?b ?d ?6 ? 4 ? ?7
下面使用 query() 來實現。
>>> df.query("A in B") ? ?A ?B ?C ? D ?E.E 0 ?e ?f ?0 ?10 ? 10 1 ?d ?b ?2 ? 8 ? ?9 2 ?c ?c ?4 ? 6 ? ?8 3 ?b ?d ?6 ? 4 ? ?7
可以看到使用 query 后的代碼簡潔易懂,并且它對于內存的消耗也更小。
多條件查詢
選取 A列字母出現在B列,并且C列小于D列 的所有行。
>>> df.query('A in B and C < D') ? ?A ?B ?C ? D ?E.E 0 ?e ?f ?0 ?10 ? 10 1 ?d ?b ?2 ? 8 ? ?9 2 ?c ?c ?4 ? 6 ? ?8
這里 and 也可以用 & 表示。
引用變量
表達式中也可以使用外部定義的變量,在變量名前用@標明。
>>> number = 5 >>> df.query('A in B & C > @number') ? ?A ?B ?C ?D ?E.E 3 ?b ?d ?6 ?4 ? ?7
索引選取
選取 A列字母出現在B列,并且索引大于2 的所有行。
>>> df.query('A in B and index > 2') ? ?A ?B ?C ?D ?E.E 3 ?b ?d ?6 ?4 ? ?7
多索引選取
創建一個兩層索引的 DataFrame。
>>> import numpy as np >>> colors = ['yellow']*3 + ['red']*2 >>> rank = [str(i) for i in range(5)] >>> index = pd.MultiIndex.from_arrays([colors, rank], names=['color', 'rank']) >>> df = pd.DataFrame(np.arange(10).reshape(5, 2),columns=['A', 'B'] , index=index) >>> df = pd.DataFrame(np.arange(10).reshape(5, 2),columns=['A', 'B'] , index=index) >>> df ? ? ? ? ? ? ?A ?B color ?rank ? ? ? yellow 0 ? ? 0 ?1 ? ? ? ?1 ? ? 2 ?3 ? ? ? ?2 ? ? 4 ?5 red ? ?3 ? ? 6 ?7 ? ? ? ?4 ? ? 8 ?9
1.當有多層索引有名稱時,通過索引名稱直接選取。
>>> df.query("color == 'red'") ? ? ? ? ? ? A ?B color rank ? ? ? red ? 3 ? ? 6 ?7 ? ? ? 4 ? ? 8 ?9
2.當有多層索引無名時,通過索引級別來選取。
>>> df.index.names = [None, None] >>> df.query("ilevel_0 == 'red'") ? ? ? ?A ?B red 3 ?6 ?7 ? ? 4 ?8 ?9 >>> df.query("ilevel_1 == '4'") ? ? ? ?A ?B red 4 ?8 ?9
特殊字符
對于列名中間有空格或運算符等其他特殊符號,需要使用反引號 ``。
>>> df.query('A == B | (C + 2 > `E.E`)') ? ?A ?B ?C ?D ?E.E 2 ?c ?c ?4 ?6 ? ?8 3 ?b ?d ?6 ?4 ? ?7 4 ?a ?e ?8 ?2 ? ?6
總的來說,query() 用法比較簡單,可以快速上手,代碼可讀性也提高了不少。
原文鏈接:https://juejin.cn/post/7055974103127162911
相關推薦
- 2023-01-20 Python如何求取逆序數_python
- 2022-07-12 Linux系統下的時區配置管理
- 2023-07-13 react中useEffect基本用法及底層機制
- 2022-04-20 ASP.NET?Core?Zero模塊系統講解_實用技巧
- 2022-12-08 C++?Boost?PropertyTree示例超詳細講解_C 語言
- 2023-06-21 Python中sorted()用法案例代碼_python
- 2022-08-03 GoFrame框架garray并發安全數組使用開箱體驗_Golang
- 2022-07-15 Python打印數據類型的全過程_python
- 最近更新
-
- 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同步修改后的遠程分支