【在熊猫中应用```apply()```时出错】弓背霞明剑照霜,秋风走马出咸阳。这篇文章主要讲述在熊猫中应用```apply()```时出错相关的知识,希望能为你提供帮助。
我正在尝试计算与数据框中各个条目相关联的百分位数(使用列中的值分布)。我确定我缺少一些[[basic东西,但无法弄清楚为什么在运行以下代码时出现错误,
from scipy.stats import percentileofscore as pctl
import pandas as pd
import numpy as npdata = https://www.songbingjia.com/android/np.arange(100).reshape(20,5)
df = pd.DataFrame(data)def f(series):
r= series.index
return pctl(series.values, series.iloc[r])df.apply(f)
这是我得到的错误,--------------------------------------------------------------------------- ValueErrorTraceback (most recent call last) < ipython-input-7-4d3ad4c6f441> in < module> ----> 1 df.apply(f)C:PythonMinicondaenvsleiaplibsite-packagespandascoreframe.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds) 6012args=args, 6013kwds=kwds) -> 6014return op.get_result() 6015 6016def applymap(self, func):C:PythonMinicondaenvsleiaplibsite-packagespandascoreapply.py in get_result(self) 316*self.args, **self.kwds) 317 --> 318return super(FrameRowApply, self).get_result() 319 320def apply_broadcast(self):C:PythonMinicondaenvsleiaplibsite-packagespandascoreapply.py in get_result(self) 140return self.apply_raw() 141 --> 142return self.apply_standard() 143 144def apply_empty_result(self):C:PythonMinicondaenvsleiaplibsite-packagespandascoreapply.py in apply_standard(self) 246 247# compute the result using the series generator --> 248self.apply_series_generator() 249 250# wrap resultsC:PythonMinicondaenvsleiaplibsite-packagespandascoreapply.py in apply_series_generator(self) 275try: 276for i, v in enumerate(series_gen): --> 277results[i] = self.f(v) 278keys.append(v.name) 279except Exception as e:< ipython-input-6-347aa35ccd44> in f(series) 1 def f(series): 2r= series.index ----> 3return pctl(series.values, series.iloc[r])C:PythonMinicondaenvsleiaplibsite-packagesscipystatsstats.py in percentileofscore(a, score, kind) 1785 1786""" -> 1787if np.isnan(score): 1788return np.nan 1789a = np.asarray(a)C:PythonMinicondaenvsleiaplibsite-packagespandascoregeneric.py in __nonzero__(self) 1574raise ValueError("The truth value of a {0} is ambiguous. " 1575"Use a.empty, a.bool(), a.item(), a.any() or a.all()." -> 1576.format(self.__class__.__name__)) 1577 1578__bool__ = __nonzero__ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0')
答案如何使用NumPy向量化解决方案:
data = https://www.songbingjia.com/android/np.arange(100).reshape(20,5)
df = pd.DataFrame(data)def f(series):
return np.percentile(series,series)df.apply(f)
推荐阅读
- 如何解决Android许可问题
- Android导航图继续api调用问题
- Android(WebView-未为会议启用摄像头和麦克风)
- 使用create-react-app创建React应用需要花费很长时间,但并不成功
- AWS面部识别试图用Android应用程序中的BasicAWSCredentials描述集合。]
- 在Google Maps android上放置点击监听器
- React app Express Api无法运行localhost 404(未找到)
- AppAuth / OpenID-Connect(使用用户名和密码登录())
- 通过单击android studio中的按钮将我的当前位置发送给所有用户