2017-12-04 6 views
0

데이터 프레임 행 전체에 수식을 적용하여 행의 수 추세를 파악하려고합니다.팬더 행/회귀 행에 수식 적용

아래 예제는 .apply 부분이 사용될 때까지 작동합니다.

df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD')) 
axisvalues=list(range(1,len(db.columns)+1)) 

def calc_slope(row): 
    return scipy.stats.linregress(df.iloc[row,:], y=axisvalues) 

calc_slope(1) # this works 

df["New"]=df.apply(calc_slope,axis=1) # this fails *- "too many values to unpack"* 

난 당신이 하나 개의 속성에 대한 필요가 있다고 생각 도움

+0

'scipy.stats.linregress는()'하나 개 이상의 값을 반환합니다. – pault

+0

그게 내가 망쳐 놓은거야. 함수에 인수를 전달하는 방식이라고 생각했습니다. 감사합니다. – Kdog

답변

2

주셔서 감사합니다 :

def calc_slope(row): 
    a = scipy.stats.linregress(row, y=axisvalues) 
    return a.slope 

df["slope"]=df.apply(calc_slope,axis=1) 
print (df) 
      A   B   C   D  slope 
0 0.444640 0.024624 -0.016216 0.228935 -2.553465 
1 1.226611 1.962481 1.103834 0.645562 -1.455239 
2 -0.259415 0.971097 0.124538 -0.704115 -0.718621 
3 1.938422 1.787310 -0.619745 -2.560187 -0.575519 
4 -0.986231 -1.942930 2.677379 -1.813071 0.075679 
5 0.611214 -0.258453 0.053452 1.223544 0.841865 
6 0.685435 0.962880 -1.517077 -0.101108 -0.652503 
7 0.368278 1.314202 0.748189 2.116189 1.350132 
8 -0.322053 -1.135443 -0.161071 -1.836761 -0.987341 
9 0.798461 0.461736 -0.665127 -0.247887 -1.610447 

그리고 모든 부가 요소에 대한

Series에 다음 dict에 튜플의 이름 및 변환합니다. 출력은 새로운 DataFrame, 그래서 경우는 원래 join 필요하다 :

np.random.seed(1997) 

df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD')) 
axisvalues=list(range(1,len(df.columns)+1)) 

def calc_slope(row): 
    a = scipy.stats.linregress(row, y=axisvalues) 
    return pd.Series(a._asdict()) 

print (df.apply(calc_slope,axis=1)) 
     slope intercept rvalue pvalue stderr 
0 -2.553465 2.935355 -0.419126 0.580874 3.911302 
1 -1.455239 4.296670 -0.615324 0.384676 1.318236 
2 -0.718621 2.523733 -0.395862 0.604138 1.178774 
3 -0.575519 2.578530 -0.956682 0.043318 0.123843 
4 0.075679 2.539066 0.127254 0.872746 0.417101 
5 0.841865 2.156991 0.425333 0.574667 1.266674 
6 -0.652503 2.504915 -0.561947 0.438053 0.679154 
7 1.350132 0.965285 0.794704 0.205296 0.729193 
8 -0.987341 1.647104 -0.593680 0.406320 0.946311 
9 -1.610447 2.639780 -0.828856 0.171144 0.768641 

df = df.join(df.apply(calc_slope,axis=1)) 
print (df) 
      A   B   C   D  slope intercept rvalue \ 
0 0.444640 0.024624 -0.016216 0.228935 -2.553465 2.935355 -0.419126 
1 1.226611 1.962481 1.103834 0.645562 -1.455239 4.296670 -0.615324 
2 -0.259415 0.971097 0.124538 -0.704115 -0.718621 2.523733 -0.395862 
3 1.938422 1.787310 -0.619745 -2.560187 -0.575519 2.578530 -0.956682 
4 -0.986231 -1.942930 2.677379 -1.813071 0.075679 2.539066 0.127254 
5 0.611214 -0.258453 0.053452 1.223544 0.841865 2.156991 0.425333 
6 0.685435 0.962880 -1.517077 -0.101108 -0.652503 2.504915 -0.561947 
7 0.368278 1.314202 0.748189 2.116189 1.350132 0.965285 0.794704 
8 -0.322053 -1.135443 -0.161071 -1.836761 -0.987341 1.647104 -0.593680 
9 0.798461 0.461736 -0.665127 -0.247887 -1.610447 2.639780 -0.828856 

    pvalue stderr 
0 0.580874 3.911302 
1 0.384676 1.318236 
2 0.604138 1.178774 
3 0.043318 0.123843 
4 0.872746 0.417101 
5 0.574667 1.266674 
6 0.438053 0.679154 
7 0.205296 0.729193 
8 0.406320 0.946311 
9 0.171144 0.768641 
+0

데이터 프레임에 누락 값이있는 경우 어떻게 동일한 작업을 수행 할 수 있습니까? linregress 함수는 모든 데이터가 없으면 반환하지 않습니다. 어떻게 든 누락 된 값을 마스크하고 행에있는 값에 대한 linregress 정보를 반환 할 수 있습니까? – Kdog

+0

불행히도 지금은 전화만으로 새로운 질문을 만들 수 있습니까? 내가 온라인으로 할거야. – jezrael

+0

고마워요! 지금 질문 게시 – Kdog