日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

pandas?實現?in?和?not?in?的用法及使用心得_python

作者:Ch3n ? 更新時間: 2023-02-27 編程語言

pandas in 和 not in 的用法

經常在處理數據中從一個總數據中清洗出數據, 但是有時候需要把沒有處理的數據也統計出來.

這時候就需要使用:

pandas.DataFrame.isin

DataFrame中的每個元素是否都包含在值中

pandas文檔位置

例子:

如何實現SQL的等價物IN和NOT IN?
我有一個包含所需值的列表。下面是一個場景:
df = pd.DataFrame({'countries':['US','UK','Germany','China']})
countries = ['UK','China']
 
# pseudo-code:
df[df['countries'] not in countries]

之前的做法是這樣:

df = pd.DataFrame({'countries':['US','UK','Germany','China']})
countries = pd.DataFrame({'countries':['UK','China'], 'matched':True})
 
# IN
df.merge(countries,how='inner',on='countries')
 
# NOT IN
not_in = df.merge(countries,how='left',on='countries')
not_in = not_in[pd.isnull(not_in['matched'])]

但上面這樣做覺得很不好, 也翻了文檔才找到比較好解決方式.

# IN
something.isin(somewhere)
 
# NOT IN
~something.isin(somewhere)

例子:

>>> df
  countries
0        US
1        UK
2   Germany
3     China
>>> countries
['UK', 'China']
>>> df.countries.isin(countries)
0    False
1     True
2    False
3     True
Name: countries, dtype: bool
>>> df[df.countries.isin(countries)]
  countries
1        UK
3     China
>>> df[~df.countries.isin(countries)]
  countries
0        US
2   Germany

ps:pandas實現in和 not in

pandas中經常會需要對某列做一些篩選,比如篩選某列里的不包含某些值的行,類似sql里的in和not in功能,那么怎么實現呢。

import pandas as pd
columns = ['name','country']
index = [1,2,3,4]
row1 = ['a','China']
row2 = ['b','UK']
row3 = ['c','USA']
row4 = ['d','HK']

df = pd.DataFrame([row1,row2,row3,row4],
                   index=index,
                   columns=columns)
df

chinese = ['China','HK']

那么想查看數據中是chines的,

df[df.country.isin(chinese)]

查看數據中不是chines的,

原文鏈接:https://ch3nnn.blog.csdn.net/article/details/91374033

欄目分類
最近更新