2014-12-29 58 views
0

我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()值

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)的員工數據(登錄)。數字列是垃圾箱。每個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是使用上述函數創建的。

我已經嘗試了各種東西,如:

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'> 

我也嘗試使用應用功能,但很可能我這樣做是錯誤的。我的猜測是,我要麼沒有正確地爲一列應用函數,要麼我得到的值不是整數。我一直在嘗試幾天,所以現在正在分解尋求幫助....

如何使用上述函數生成列,以便獲取特定於行的數據?

+0

你可以發佈你的HMDrr df的數據,你也可以通過查看可能是df的'y'類型以及'.count()'返回什麼類型來進行調試,這有點令人困惑 – EdChum

+0

@EdChum,感謝偉大的觀點。我發佈了一個df示例。我在'y'上做了'dstypes',發現它是一個系列對象。 '.count'確實返回了花車。我在這個問題中修改了這些函數,以便'y'是一個numpy.ndarray。 –

回答

0

雖然也許不是最好的辦法做到這一點,我解決它與以下。

內置一個calc_linear函數返回斜率和截距:

#Caculate logarithimic slope and intercept in HMDrr Dataset 
def calc_linear(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, intercept 

創建用於數據空列:

#Create metric columns 
HMDrr['SLOPE'] = "" 
HMDrr['INTERCEPT'] = "" 

RAN中的for循環來填充列:

#For loop to calculate metrics 
for x in range(0,HMDrr.SLOPE.count()): 
    values = calc_linear(x) 
    HMDrr.SLOPE[x] = values[0] 
    HMDrr.INTERCEPT[x] = values[1] 

如果有更清潔的方式,那麼我很樂意聽到它:)