'str'对象不能解释为groupby上的整数
Can someone please explain why I'm getting this error when I'm doing a lingress (slope) on 'day' and 'value' which are both numeric datatype. Below is my script:
有人可以解释为什么我在'day'和'value'上做一个lingress(斜坡)时都会出现这个错误,这两个都是数值数据类型。以下是我的脚本:
import pandas as pd
from scipy.stats import linregress
y = pd.DataFrame({'entity':['a','a','b','b','b','c'],
'day':[1999,2004,2003,2007,2014, 2016],
'value':[2,5,3,2,7,8]})
mylist= ['a', 'b']
y1 = y.groupby('entity').apply(lambda x: x[x['entity'].isin(mylist)])
This line gives error:
这一行给出错误:
y1.apply(lambda v: linregress(v['day'], v['value']))
error trace:
TypeError Traceback (most recent call last) /anaconda3/lib/python3.6/site-packages/pandas/core/indexes/multi.py in get_value(self, series, key) 999 try: -> 1000 return libindex.get_value_at(s, k) 1001 except IndexError:
TypeError Traceback(最近一次调用最后一次)/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/multi.py in get_value(self,series,key)999试试: - > 1000 return libindex.get_value_at( s,k)1001除了IndexError:
pandas/_libs/index.pyx in pandas._libs.index.get_value_at()
pandas._libs.index.get_value_at()中的pandas / _libs / index.pyx
pandas/_libs/src/util.pxd in util.get_value_at()
util.get_value_at()中的pandas / _libs / src / util.pxd
TypeError: 'str' object cannot be interpreted as an integer
TypeError:'str'对象不能解释为整数
During handling of the above exception, another exception occurred:
在处理上述异常期间,发生了另一个异常:
KeyError Traceback (most recent call last) in () ----> 1 y1.apply(lambda v: linregress(v['day'], v['value'])) 2
KeyError Traceback(最近一次调用last)in()----> 1 y1.apply(lambda v:linregress(v ['day'],v ['value']))2
KeyError: ('day', 'occurred at index entity')
KeyError :('day','发生在索引实体')
2 个解决方案
#1
0
Update, then you need this:
更新,然后你需要这个:
y1.groupby(level='entity').apply(lambda x: linregress(x['day'],x['value']))
Output:
entity
a (0.6, -1197.3999999999999, 1.0, 0.0, 0.0)
b (0.4032258064516129, -805.6774193548387, 0.8485552916276634, 0.35494576760559776, 0.25142673013096595)
dtype: object
I think all you need is to do this, you do not need to apply, just pass the two dataframe columns to linregress
:
我认为您只需要执行此操作,您不需要应用,只需将两个数据帧列传递给linregress:
linregress(y1['day'],y1['value'])
Output:
LinregressResult(slope=0.29073482428115016, intercept=-579.2396166134187, rvalue=0.7502746874224853, pvalue=0.14406233411953523, stderr=0.1479110164470003)
更多相关文章
- 套接字错误“IP地址在其上下文中无效” - Python
- 无法安装ndg-httpsclient或者我的解决方案错误
- Python套接字代理示例,不断收到调用bind()的错误..为什么?
- 是什么导致了Python分割错误?
- wget在linux中安装出现错误解决办法
- busybox1.19.3编译错误解决办法
- 打开“保存命令历史记录时遇到错误”时的Matlab错误
- 编译android源码时,jack server 出现out of memory error 错误的
- 如何从PHP的mail()失败中获取额外的错误信息?