如何使用pandas将数据正确地输入到函数中?下面的代码当前导致错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
我要做的就是创建一个输出矩阵,其中的字符串受函数中的条件语句约束。
def mixtapeFire(timesPlayed, rating):
if timesPlayed >=1000 & rating >=3:
print('Your mixtape is fire!')
if rating >5:
print('Invalid Input. Play Again.')
else:
print('You should quit the rap game.')
input1 = pd.DataFrame([900,2000,1001,500,4000])
input2 = pd.DataFrame([3,4,3,1,2])
for x in range(1,5):
output = pd.DataFrame(mixtapeFire(input1.iloc[x,:],input2.iloc[x,:]))
答案 0 :(得分:0)
sh -c
对系列进行比较会得到多个值。
a = pd.DataFrame([900,2000,1001,500,4000])
b = pd.DataFrame([3,4,3,1,2])
根据需要使用In [12]: a >= 1000
Out[12]:
0
0 False
1 True
2 True
3 False
4 True
In [13]: b >= 3
Out[13]:
0
0 True
1 True
2 True
3 False
4 False
In [14]: q = (a >= 1000) & (b >= 3)
In [15]: q
Out[15]:
0
0 False
1 True
2 True
3 False
4 False
或.any()
。
.all()
因为结果中有多个In [16]: q.any()
Out[16]:
0 True
dtype: bool
In [17]: q.all()
Out[17]:
0 False
dtype: bool
值,所以 无法判断整个系列是True/False
还是True
。
False
答案 1 :(得分:0)
根据您的需要,可以将任何“ a”方法应用于您的参数input1.iloc [x,:],input2.iloc [x ,:]。例如:
output = pd.DataFrame(mixtapeFire(input1.iloc[x,:].all(),input2.iloc[x,:].all()))
您可以从官方文档https://pandas.pydata.org/pandas-docs/stable/reference/series.html
中了解有关熊猫访问方法的更多信息。但是简而言之:您需要更具体地说明要解析的值。