![](/img/trans.png)
[英]Separate row of a dataframe based on a condition by renaming it in pandas / python
[英]Python Pandas dataframe row entry is not comparable by condition
我读入了带有数据的 csv 文件。 一切正常。 我可以做一个任务
df.loc[(df["BID"] == 7249)
但我想对“Testschritt”做同样的事情
df.loc[(df["Testschritt"] == "F1")
但我所有的条目都是错误的。 但是您可以清楚地看到,有些条目应该是true 。
我使用 jupyter 笔记本。
这里是完整的代码:
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("<FILELOCATION>", ";")
df.loc[(df["Testschritt"] == "F1")
请指教,谢谢
在F1
之前似乎有一些空间,例如' F1'
,您可以像这样删除它们:
df.loc[df["Testschritt"].str.strip() == "F1"]
或者 ypu 可以分配 output 回来:
df["Testschritt"] = df["Testschritt"].str.strip()
df.loc[df["Testschritt"] == "F1"]
如果您不想完全匹配,也可以使用str.contains() 。
df["Testschritt"].str.contains("F1")
你可以做一个简单的事情,比如
df[df['Testschritt']=='F1']
获取包含所有 F1 值的 dataframe Testschritt 列。 或得到真假结果
这样做
df['Testschritt'] == 'F1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.