我有一个大麻数据集,它有一个“效果”列,我正试图为不包含某些效果的毒株添加一个二进制“nice_-buds”列。代码如下:
nice_buds = []
undesired_effects = ["Sleepy", "Hungry", "Giggly", "Tingly", "Aroused", "Talkative"]
for row in sample["Effects"]:
if "Sleepy" not in row and "Hungry" not in row and "Giggly" not in row and "Tingly" not in row and "Aroused" not in row and "Talkative" not in row:
nice_buds.append(1)
else:
nice_buds.append(0)
sample["nice_buds"] = nice_buds
到目前为止,undesired_effects
列表什么也没做,代码在提供所需输出方面工作得非常好
我的问题是,是否有一种更为“Pythonic”或“DRY”的方法来解决这个问题……
可以将
all()
与生成器表达式一起使用,以简化if语句或使用
any()
&;检查是否存在影响:给定一个数据帧
sample
for row in sample["Effects"]
告诉我您正在使用数据帧。千万不要使用for-loop
到iterate through a dataframe李>display(sample)
相关问题 更多 >
编程相关推荐