Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何迭代函数的输入_Python_Pandas - Fatal编程技术网

Python 如何迭代函数的输入

Python 如何迭代函数的输入,python,pandas,Python,Pandas,如何使用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 & rati

如何使用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,:]))
对序列进行比较会产生多个值

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

In [18]: bool(q)
Traceback (most recent call last):

  File "<ipython-input-18-6fd0a485fec6>", line 1, in <module>
    bool(q)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 955, in __nonzero__
    .format(self.__class__.__name__))

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
[18]中的
bool(q)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
布尔(q)
文件“C:\ProgramData\Anaconda3\lib\site packages\pandas\core\generic.py”,第955行,非零__
.format(self.\uuuuuu class.\uuuuuuu.\uuuuuu name.\uuuuuuuuu))
ValueError:数据帧的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。

根据需要,您可以对参数input1.iloc[x,:],input2.iloc[x,:]应用任何“a”方法。例如:

output=pd.DataFrame(mixtapeFire(input1.iloc[x,:].all(),input2.iloc[x,:].all())

您可以从官方文档中了解有关熊猫访问方法的更多信息


但简而言之:您需要更具体地确定要解析哪些值

请修改示例的缩进。非常感谢!这是一个完美的解决方案,但我想知道是否有办法做到这一点并保留该功能。例如,如果一个函数要复杂得多,那么复制它可能需要很长时间。您只需要应用逻辑和比较值来满足您的需要。从您的问题或示例中不清楚您的规格/愿望是什么。是的,对不起,现在我再次看它,它不是很清楚。简而言之,我想学习如何在给定函数分类器(如决策树)和多个输入的情况下执行多类分类。我们的目标是生成一个带有字符串标签的输出数组,这将告诉我们什么是什么。也许您应该删除此问题,并询问一个正确反映您的问题/问题的问题。
In [18]: bool(q)
Traceback (most recent call last):

  File "<ipython-input-18-6fd0a485fec6>", line 1, in <module>
    bool(q)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 955, in __nonzero__
    .format(self.__class__.__name__))

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().