我使用pandas将下表作为Python中的数据帧加载
+--------+-------+------+
| Number | Col1 | Col2 |
+--------+-------+------+
| ABC | TRUE | SFG |
| BCD | TRUE | |
| CDE | FALSE | SFG |
| DEF | FALSE | |
| FEG | TRUE | JJI |
+--------+-------+------+
数字,Col2-字符串;Col1-布尔值
我想使用以下逻辑从df
中选择行
IF Col1 = TRUE and Col2 is not null Select Number + "," + Col2
ELSE IF Col1 = TRUE and Col2 is null Select Number
ELSE IF Col2 is not null and Col1 = FALSE Select Col2
在上述情况下,输出应该是具有以下值的列表
["ABC", "SFG", "BCD", "FEG", "JJI"] //Removing the repetition too ("SFG")
如何使用Pandas在Python中实现这个逻辑?你知道吗
使用
where
+stack
+tolist
获取唯一列表
以下是查询的多个步骤实现:
result
是(与@MaxU预测相同)相关问题 更多 >
编程相关推荐