【发布时间】:2014-12-29 05:00:45
【问题描述】:
我的df如下
BINS
SKILL object
LOGIN object
50.0 float64
100.0 float64
150.0 float64
200.0 float64
250.0 float64
300.0 float64
350.0 float64
400.0 float64
450.0 float64
500.0 float64
550.0 float64
600.0 float64
650.0 float64
700.0 float64
750.0 float64
800.0 float64
850.0 float64
900.0 float64
950.0 float64
1000.0 float64
dtype: object
这里是使用的数据示例:HMDrr.head().values
array([[‘Skill1’, ‘loginA’, 0.07090909090909091, 0.25, 0.35,
0.147619047619047616, 0.057823529411764705, 0.0,
0.0, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan],
[‘Skill1’, ‘loginB’, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],
[‘Skill1’, ‘loginC’, 0.15, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],
[‘Skill1’, ‘loginD’, 0.3333333333333333,
0.1857142857142857, 0.0, 0.15, 0.1, 0.0, 0.05666666666666667,
0.06692307692307693, 0.05692307692307693, 0.13529411764705882, 0.1,
0.0, nan, nan, nan, nan, nan, nan, nan, nan],
[‘Skill1’, ‘loginE’, 0.1, 0.0, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]], dtype=object)
我有按工作类型 (SKILL) 划分的员工数据 (LOGIN)。数字列是箱。每个 bin 包含第 50 次交互的性能结果,然后是 100 次,依此类推。我需要计算 SKILL 和 LOGIN 的斜率和截距,以便创建新的员工绩效提升计划。
为此,我构建了以下内容:
#Bins for contacts
startBin = 0.0
stopBin = 1000.0
incrementBin = 50.0
sortBins = np.arange(startBin, stopBin + incrementBin, incrementBin)
binLabels = np.arange(startBin + incrementBin, stopBin + incrementBin, incrementBin)
#Caculate logarithimic slope in HMDrr Dataset
def calc_slope(z):
y = HMDrr.loc[z,binLabels].dropna()
number = y.count()+1
y = y.values.astype(float)
x = np.log(range(1,number,1))
slope, intercept, r, p, stderr = linregress(x, y)
return slope
#Caculate logarithimic intercept in HMDrr Dataset
def calc_intercept(z):
y = HMDrr.loc[z,binLabels].dropna()
number = y.count()+1
y = y.values.astype(float)
x = np.log(range(1,number,1))
slope, intercept, r, p, stderr = linregress(x, y)
return intercept
当我通过手动放置 z 值运行时,它运行良好:
calc_slope(10)
-0.018236067481219649
我想在使用上述函数创建的 df 中创建 SLOPE 和 INTERCEPT 列。
我尝试了很多方法,例如:
HMDrr['SLOPE'] = calc_slope(HMDrr.index)
TypeError Traceback (most recent call last)
<ipython-input-717-4a58ad29d7b0> in <module>()
----> 1 HMDrr['SLOPE'] = calc_slope(HMDrr.index)
<ipython-input-704-26a18390e20c> in calc_slope(z)
7 def calc_slope(z):
8 y = HMDrr.loc[z,binLabels].dropna()
----> 9 x = np.log(range(1,y.count()+1,1))
10 slope, intercept, r, p, stderr = linregress(x, y)
11 return slope
C:\Anaconda\lib\site-packages\pandas\core\series.pyc in wrapper(self)
67 return converter(self.iloc[0])
68 raise TypeError(
---> 69 "cannot convert the series to {0}".format(str(converter)))
70 return wrapper
71
TypeError: cannot convert the series to <type 'int'>
我也尝试过使用 apply 功能,但很可能我做错了。我的猜测是我要么没有为列正确应用函数,要么我得到的值不是整数。我已经尝试了好几天,所以现在要崩溃寻求帮助....
如何使用上述函数生成列以便获取行特定数据?
【问题讨论】:
-
您可以为您的 HMDrr df 发布数据吗,您也可以通过查看可能是 df 的
y的类型以及.count()返回的类型来调试它,这是一个有点混乱 -
@EdChum,感谢您的宝贵意见。我发布了一个 df 样本。我在
y上做了一个dstypes,发现它是一个系列对象。.count确实返回了浮点数。我修改了问题中的这些函数,以便y是一个 numpy.ndarray。
标签: python python-2.7 pandas dataframe calculated-columns