Python Pandas dataframe row entry is not comparable by condition

Viewed 45

i read in a csv file with data. Everything works fine. I can do a task like

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

But I would like to do the same with "Testschritt" like

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

But all my entrys are false. But you can clearly see, that some entrys should be true.

I use the jupyter notebook.

Here the full code:

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

Output see in attached picturesOutput 1 And here Output 2

Please give advise, Thank you

3 Answers

It seems some sopaces before F1 like ' F1', you can remove them like:

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

Or ypu can assign output back:

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

You could also use str.contains() if you don't want exact match.

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

you can do a simple thing like

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

to get the dataframe Testschritt column containing all the F1 values. or to get the true False results

do like this

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