สมมติว่าคุณมีชุดข้อมูลและผลลัพธ์สำหรับการดำเนินการบูลีน
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
วิธีแก้ปัญหา
ในการแก้ปัญหานี้ เราจะปฏิบัติตามแนวทางด้านล่าง
-
กำหนดซีรีส์
-
สร้างชุดที่มีค่าบูลีนและน่าน
-
ดำเนินการบูลีน 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