我对python很陌生。我试图使用带有2个条件的'if'语句遍历列表列。我一直得到的return语句是“ Series的真值不明确(当使用if语句遍历Series时”
下面是我的代码(数据框包含来自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.iterrows()
来完成它。
这里是如何使用它的示例:
medical_app_ratings = []
for i, row in df.iterrows():
rating = row.Rating
genre = row.Genres
if genre == "Medical":
medical_app_ratings.append(rating)