Я пытаюсь изменить серию объекта DataFrame pandas с помощью функции iterrows ().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)
Первый изменяетDataFrame, 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
Я что-то упустил здесь?Минимальная переменная - это просто число с плавающей точкой, и сравнение должно пройти.Я даже попытался с помощью
int(minimum)
, но все равно выдает ту же ошибку.Также я могу использовать:
print(other_copy.loc[index] > minimum)
, и это также работает для печати правильного ответа.Есть идеи, почему это может происходить?Извините, если это что-то простое.