DataFrame 按列篩選數據


轉自:https://blog.csdn.net/butterfly1107/article/details/82756902

1.==與!=

lc.loc[lc["grade"] == "B"].head()
lc.loc[lc["grade"] != "B"].head()

2.filter函數

https://blog.csdn.net/weixin_44668131/article/details/99437698

DataFrame.filter(items=None, like=None, regex=None, axis=None)
#items對列進行篩選
#regex表示用正則進行匹配
#like進行篩選
#axis=0表示對行操作,axis=1表示對列操作

前三個參數是互斥的,只能出現一個。

#items對列進行篩選,只保留items中的列
df.filter(items=['one', 'three'])
         one  three
teacher   1      3
student   4      6
#regex表示用正則進行匹配,對列名匹配正則表達式
df.filter(regex='e$', axis=1)
         one  three
teacher   1      3
student   4      6
#like進行篩選
df.filter(like='ent', axis=0)
          one  two  three
student    4    5    6

3.isin()函數

https://blog.csdn.net/weixin_39962394/article/details/112113656

class_23 = titanic[titanic["Pclass"].isin([2, 3])]

可根據列值篩選出一個子集。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM