[英]Print certain columns with certain values from CSV file using pandas
I am trying to currently using the Iris classification dataset with Python and Pandas to do some data analysis.我目前正在尝试使用带有 Python 和 Pandas 的 Iris 分类数据集进行一些数据分析。 I'm trying to print all the petal-length values that are greater than 1.7 and where the data species is Setosa but I'm having a problem.
我正在尝试打印所有大于 1.7 且数据种类为 Setosa 的花瓣长度值,但我遇到了问题。
I keep getting this error:我不断收到此错误:
ValueError: The truth value of a Series is ambiguous. ValueError:Series 的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all().
使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
Here is my code:这是我的代码:
iris = pd.read_csv(r"C:\Users\username\Downloads\iris.csv")
for row in iris:
if iris["species"] == "setosa":
if iris["petal_width"] > 1.7:
print(iris["petal_width"])
Any help or suggestions you can give me I would greatly appreciate it.您能给我的任何帮助或建议,我将不胜感激。 Thank you!
谢谢!
print(iris.query("petal_length>1.7 & species=='setosa'")['petal_length'])
From Anurag's comment来自阿努拉格的评论
print(iris.loc[(iris["species"] == "setosa") & (iris["petal_width"] > 1.7),"petal_width"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.