编写一个 Pyton 程序以对给定的序列执行布尔逻辑 AND、OR、Ex-OR 操作
假设您有一个数列和布尔运算的结果,
And operation is: 0 True 1 True 2 False dtype: bool Or operation is: 0 True 1 True 2 True dtype: bool Xor operation is: 0 False 1 False 2 True dtype: bool
解决方案
为了解决这个问题,我们将采取以下方法。
定义一个数列
用布尔值和 nan 值创建一个数列
对下面定义的数列中的每个元素执行布尔 True 与位运算 & 的运算,
series_and = pd.Series([True, np.nan, False], dtype="bool") & True
对下面定义的数列中的每个元素执行布尔 True 与位运算 | 的运算,
series_or = pd.Series([True, np.nan, False], dtype="bool") | True
对下面定义的数列中的每个元素执行布尔 True 与位运算 ^ 的运算,
series_xor = pd.Series([True, np.nan, False], dtype="bool") ^ True
示例
让我们了解一下完整的实现,以便更好地理解 −
import pandas as pd
import numpy as np
series_and = pd.Series([True, np.nan, False], dtype="bool") & True
print("And operation is: \n",series_and)
series_or = pd.Series([True, np.nan, False], dtype="bool") | True
print("Or operation is: \n", series_or)
series_xor = pd.Series([True, np.nan, False], dtype="bool") ^ True
print("Xor operation is: \n", series_xor)输出
And operation is: 0 True 1 True 2 False dtype: bool Or operation is: 0 True 1 True 2 True dtype: bool Xor operation is: 0 False 1 False 2 True dtype: bool
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP