小编NJL*_*NJL的帖子

pandas 中的条件方法链接

在使用 pandas 方法链接时,是否有一种简单的通用方法可以使方法以 if 语句为条件?

模拟示例:

df = pd.DataFrame({'A':['one', 'two'], 'B':['one', 'two']})

change_to_numeric = False

df = (df
    .query("A == 'one'")
    .replace('one', 1)         # <-- Execute this row only "if change_to_numeric == True"
)
Run Code Online (Sandbox Code Playgroud)

谢谢你!

python if-statement method-chaining pandas

3
推荐指数
1
解决办法
610
查看次数

标签 统计

if-statement ×1

method-chaining ×1

pandas ×1

python ×1