【发布时间】:2020-07-17 13:18:44
【问题描述】:
我对 python 很陌生。我正在尝试使用具有 2 个条件的“if”语句遍历列表列。我不断得到的返回语句是“一个系列的真值是不明确的(当使用 if 语句迭代一个系列时”
以下是我的代码(数据框由来自 Google Play 商店的应用数据组成)
import pandas as pd
df = pd.read_csv("googleplaystore.csv")
df.tail()
df["Rating"].fillna(value = '0.0', inplace= True)
medical_app_ratings = []
for row in df[1:]:
rating = df.Rating
genre = df.Genres
if genre == "Medical":
medical_app_ratings.append(rating)
ValueError Traceback (most recent call last)
<ipython-input-154-51c7806c01da> in <module>
5 rating = df.Rating
6 genre = df.Genres
----> 7 if genre == "Medical":
8 medical_app_ratings.append(rating).bool()
9
~\anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1477 def __nonzero__(self):
1478 raise ValueError(
-> 1479 f"The truth value of a {type(self).__name__} is ambiguous. "
1480 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1481 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
如果有人能告诉我我做错了什么,我将不胜感激。
【问题讨论】:
-
你想要:
df.loc[df['Genres'] == 'Medical', 'Rating']。在处理dataframes时,尽量摆脱iterating的想法,使用pandas和numpy提供的向量化方法。
标签: python pandas if-statement iteration valueerror