【问题标题】:How to replace data in csv file based on criteria?如何根据条件替换csv文件中的数据?
【发布时间】:2020-06-15 17:17:54
【问题描述】:

我想将WinorLoss 列中的所有数据从 W/L 替换为 1/0(如果 W,则为 1,如果 L,则为 0)..

WinorLoss 是我的 csv 文件中的一列,其中填充了对应游戏的 W 或 L

我尝试了以下方法:

if df.WinorLoss == "W":
 df.WinorLoss == "1"
else:
 df.WinorLoss == "0"

出现以下错误: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我是 python 新手,所以不是 100% 如何去做这件事.. 任何帮助将不胜感激?

【问题讨论】:

标签: python pandas if-statement jupyter


【解决方案1】:

试试这个:

df.WinorLoss.map(lambda x: "1" if x == "W" else "0")

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 2020-09-15
    • 2022-01-19
    • 2019-05-20
    相关资源
    最近更新 更多