Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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_Numpy - Fatal编程技术网

Python 无法使用简单的条件语句修改数据帧。但在使用静态数字时有效

Python 无法使用简单的条件语句修改数据帧。但在使用静态数字时有效,python,pandas,numpy,Python,Pandas,Numpy,我试图使用iterrows()函数更改pandas DataFrame对象的系列。数据帧充满了随机浮动。下面是两段代码的示例: 这一个有效: for index,row in other_copy.iterrows() other_copy.loc[index] = (other_copy.loc[index] > 30) 但这一条没有: for index,row in other_copy.iterrows(): top_3 = other_copy.loc[index

我试图使用iterrows()函数更改pandas DataFrame对象的系列。数据帧充满了随机浮动。下面是两段代码的示例:

这一个有效:

for index,row in other_copy.iterrows()
    other_copy.loc[index] = (other_copy.loc[index] > 30)
但这一条没有:

for index,row in other_copy.iterrows():
   top_3 = other_copy.loc[index].nlargest(3)
   minimum = min(top_3)
   other_copy.loc[index] = (other_copy.loc[index] > minimum)
第一个修改数据帧,True和False相应地修改。但是,第二个给了我以下错误:

> TypeError                                 Traceback (most recent call last) <ipython-input-116-11f6c908f54a> in <module>()
      1 for index,row in other_copy.iterrows():
----> 2     top_3 = other_copy.loc[index].nlargest(3)
      3     minimum = min(top_3)
      4     other_copy.loc[index] = (other_copy.loc[index] > minimum)

/opt/conda/lib/python3.6/site-packages/pandas/core/series.py in
nlargest(self, n, keep)    2061         dtype: float64    2062        
"""
-> 2063         return algorithms.SelectNSeries(self, n=n, keep=keep).nlargest()    2064     2065     def nsmallest(self, n=5,
keep='first'):

/opt/conda/lib/python3.6/site-packages/pandas/core/algorithms.py in
nlargest(self)
    915 
    916     def nlargest(self):
--> 917         return self.compute('nlargest')
    918 
    919     def nsmallest(self):

/opt/conda/lib/python3.6/site-packages/pandas/core/algorithms.py in
compute(self, method)
    952             raise TypeError("Cannot use method '{method}' with "
    953                             "dtype {dtype}".format(method=method,
--> 954                                                    dtype=dtype))
    955 
    956         if n <= 0:

TypeError: Cannot use method 'nlargest' with dtype object
但它仍然给我同样的错误。我还可以使用:

print(other_copy.loc[index] > minimum)

这也可以打印正确的响应。你知道为什么会这样吗?如果这是一个简单的问题,很抱歉。

问题不是
最小值
,而是设置
最小值的代码。当您切出行时,它会变成一个系列,其中包含一个数据类型
对象
(因为列中有混合的数据类型,
对象
数据类型是唯一与所有列兼容的类型)

当您尝试在此行切片上运行
.nlargest()
时,它清楚地告诉您问题:
TypeError:无法将方法“nlargest”与dtype对象一起使用
因此,您应该将序列转换为数字

import pandas as pd

for index,row in other_copy.iterrows():
   top_3 = pd.to_numeric(other_copy.loc[index], errors = 'coerce').nlargest(3)
   minimum = min(top_3)
   other_copy.loc[index] = (other_copy.loc[index] > minimum)

如果行中没有可以转换为数字的条目,这可能会导致另一个错误,如果尝试进行不安全的比较(如
'str'
>
'float'

.nlargest()
用于数据帧/列,不是在单独的一行上。你想用你的函数做什么?检查索引是否大于/小于第三大索引号?是的,每行都有一组值,我得到每行的第三大值,低于该值的所有值都将标记为False。前3个最大值将被标记为True。换句话说,它是在数据帧中逐行进行的,并根据它们是否满足条件将值从float更改为True和False。同样,当我在条件中放入一个正常数字而不是“minimum”变量时,它可以正常工作。最后的“最小”变量也是一个正常数。只是因为某些原因它不起作用。非常感谢!我现在明白了,谢谢你澄清原因。:)
import pandas as pd

for index,row in other_copy.iterrows():
   top_3 = pd.to_numeric(other_copy.loc[index], errors = 'coerce').nlargest(3)
   minimum = min(top_3)
   other_copy.loc[index] = (other_copy.loc[index] > minimum)