【问题标题】:Python Pandas dataframe row entry is not comparable by conditionPython Pandas 数据框行条目不能按条件进行比较
【发布时间】:2020-11-25 10:25:04
【问题描述】:

我读入了一个带有数据的 csv 文件。一切正常。 我可以做这样的任务

df.loc[(df["BID"] == 7249)

但我想对“Testschritt”做同样的事情

df.loc[(df["Testschritt"] == "F1")

但我所有的条目都是 false。但是你可以清楚地看到,有些条目应该是true

我用的是 jupyter notebook。

这里是完整的代码:

import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("<FILELOCATION>", ";")
df.loc[(df["Testschritt"] == "F1")

输出见附图Output 1 在这里Output 2

请指教,谢谢

【问题讨论】:

    标签: python pandas dataframe csv data-science


    【解决方案1】:

    F1 之前似乎有一些空格,比如' F1',你可以像这样删除它们:

    df.loc[df["Testschritt"].str.strip() == "F1"]
    

    或者ypu可以分配回输出:

    df["Testschritt"] = df["Testschritt"].str.strip()
    df.loc[df["Testschritt"] == "F1"]
    

    【讨论】:

      【解决方案2】:

      如果您不想完全匹配,也可以使用str.contains()

      df["Testschritt"].str.contains("F1")

      【讨论】:

        【解决方案3】:

        你可以做一个简单的事情,比如

        df[df['Testschritt']=='F1']

        获取包含所有 F1 值的数据框 Testschritt 列。 或得到真假结果

        这样做

        df['Testschritt'] == 'F1'
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-13
          • 2018-07-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多