Pandas – 获取其他系列中不存在的系列元素
有时我们有两个或多个系列,我们必须找到一个系列中存在但另一个系列中不存在的所有元素。我们可以轻松地做到这一点,使用按位非运算符和pandas.isin()函数。
示例 1:取两个整数系列
Python3
# Importing pandas library
import pandas as pd
# Creating 2 pandas Series
ps1 = pd.Series([2, 4, 8, 20, 10, 47, 99])
ps2 = pd.Series([1, 3, 6, 4, 10, 99, 50])
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
# Using Bitwise NOT operator along
# with pandas.isin()
print("\nItems of ps1 not present in ps2:")
res = ps1[~ps1.isin(ps2)]
print(res)
Python3
# Importing pandas library
import pandas as pd
# Creating 2 pandas Series
ps1 = pd.Series([2.8, 4.5, 8.0, 2.2, 10.1, 4.7, 9.9])
ps2 = pd.Series([1.4, 2.8, 4.7, 4.8, 10.1, 9.9, 50.12])
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
# Using Bitwise NOT operator along
# with pandas.isin()
print("\nItems of ps1 not present in ps2:")
res = ps1[~ps1.isin(ps2)]
print(res)
Python3
# Importing pandas library
import pandas as pd
# Creating 2 pandas Series
ps1 = pd.Series(['Monu', 'Sonu', 'Tonu', 'Nonu',
'Ronu', 'Bonu'])
ps2 = pd.Series(['Sweetu', 'Tweetu', 'Nonu',
'Micku', 'Bonu', 'Kicku'])
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
# Using Bitwise NOT operator along with
# pandas.isin()
print("\nItems of ps1 not present in ps2:")
res = ps1[~ps1.isin(ps2)]
print(res)
输出:
在上面的示例中,我们采用 2 个int类型“ ps1 ”和“ ps2 ”的熊猫系列,并找到 ps1 中不存在于 ps2 中的所有元素。
示例 2:取两个浮点系列
蟒蛇3
# Importing pandas library
import pandas as pd
# Creating 2 pandas Series
ps1 = pd.Series([2.8, 4.5, 8.0, 2.2, 10.1, 4.7, 9.9])
ps2 = pd.Series([1.4, 2.8, 4.7, 4.8, 10.1, 9.9, 50.12])
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
# Using Bitwise NOT operator along
# with pandas.isin()
print("\nItems of ps1 not present in ps2:")
res = ps1[~ps1.isin(ps2)]
print(res)
输出:
在上面的示例中,我们采用浮点类型“ ps1 ”和“ ps2 ”的 2 个熊猫系列,并找到 ps1 中不存在于 ps2 中的所有元素。
示例 3:取两个字符串系列
蟒蛇3
# Importing pandas library
import pandas as pd
# Creating 2 pandas Series
ps1 = pd.Series(['Monu', 'Sonu', 'Tonu', 'Nonu',
'Ronu', 'Bonu'])
ps2 = pd.Series(['Sweetu', 'Tweetu', 'Nonu',
'Micku', 'Bonu', 'Kicku'])
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
# Using Bitwise NOT operator along with
# pandas.isin()
print("\nItems of ps1 not present in ps2:")
res = ps1[~ps1.isin(ps2)]
print(res)
输出:
在上面的示例中,我们采用字符串类型 ' ps1 ' 和 ' ps2 ' 的 2 个熊猫系列,并找到 ps1 中不存在于 ps2 中的所有元素。