2017-07-31 9 views
0

与えられたデータフレーム(ベクトルxの値を含むdfTest)のすべての行に基本的なスプライン関数を適用してより大きなもの(dfBigger)を取得しようとしています。ベクトルxnew(xを含む)のすべての値を含みます。pandasデータフレームに適用すると、渡された値のエラーが発生する

と基本的なスプライン関数:

def spline(y, x , xnew): 
    from scipy import interpolate 
    model = interpolate.splrep(x,y, s=0.) 
    ynew = interpolate.splev(xnew,model) 
    result = ynew.round(3) 
    return result 

に動作するようです:

私は、したがって、以下の変数を定義

spline(dfTest.iloc[0],x,xnew) 
Out[176]: array([ 0.696, 0.286, 0.161, 0.227, 0.388, 0.551]) 

を、私はそれを適用しようとすると、すべての行で:

dfBigger = dfTest.apply(lambda row : spline(row, x, xnew), axis = 1) 

私はこれだ:dfBiggerサイズがどこにも定義されていないように私が間違っているものを見ることができない

ValueError: Shape of passed values is (3, 6), indices imply (3, 4) 

を。このコードについての助言やコメントがあれば幸いです。

答えて

3

df.apply(func) から返された新しいシリーズまたはデータフレームをfuncで返します。 SeriesまたはDataFrameの形状は、funcによって返される 値の種類によって異なります。どのように振る舞うdf.apply、以下の呼び出しと 実験でよりよいハンドルを取得するには:だから

dfTest.apply(lambda row: 1, axis=1)      # Series 
dfTest.apply(lambda row: [1], axis=1)      # Series 
dfTest.apply(lambda row: [1,2], axis=1)     # Series 
dfTest.apply(lambda row: [1,2,3], axis=1)     # Series 
dfTest.apply(lambda row: [1,2,3,4], axis=1)    # Series 
dfTest.apply(lambda row: [1,2,3,4,5], axis=1)    # Series 

dfTest.apply(lambda row: np.array([1]), axis=1)   # DataFrame 
dfTest.apply(lambda row: np.array([1,2]), axis=1)   # ValueError 
dfTest.apply(lambda row: np.array([1,2,3]), axis=1)  # ValueError 
dfTest.apply(lambda row: np.array([1,2,3,4]), axis=1)  # DataFrame! 
dfTest.apply(lambda row: np.array([1,2,3,4,5]), axis=1) # ValueError 

dfTest.apply(lambda row: pd.Series([1]), axis=1)   # DataFrame 
dfTest.apply(lambda row: pd.Series([1,2]), axis=1)  # DataFrame 
dfTest.apply(lambda row: pd.Series([1,2,3]), axis=1)  # DataFrame 
dfTest.apply(lambda row: pd.Series([1,2,3,4]), axis=1) # DataFrame 
dfTest.apply(lambda row: pd.Series([1,2,3,4,5]), axis=1) # DataFrame 

を規則は、我々は、これらの実験から引き出すことができますか?

  • funcがスカラーまたはリストを返した場合、df.apply(func)は系列を返します。
  • funcがSeriesを返した場合、df.apply(func)はDataFrameを返します。
  • funcが1D NumPy配列を返した場合、およびの配列に要素が1つしかない場合、df.apply(func)はDataFrameを返します。 funcは1D numpyの配列を返す場合dfがデータフレームを返すdf.apply(func)列を有するもの(不ひどく有用場合...)
  • 配列は同じ数の要素を有しています。

    def spline(y, x, xnew): 
        ... 
        return pd.Series(result) 
    

    :(便利な、限られた)

func以来戻っ6つの値、そしてあなたは、結果としてデータフレームをしたい、 ソリューションはfunc持ってnumpyの配列の代わりにシリーズを返すことです

import numpy as np 
import pandas as pd 
from scipy import interpolate 

def spline(y, x, xnew): 
    model = interpolate.splrep(x,y, s=0.) 
    ynew = interpolate.splev(xnew,model) 
    result = ynew.round(3) 
    return pd.Series(result) 

x = [0,1,3,5] 
xnew = range(0,6) 
np.random.seed(123) 
dfTest = pd.DataFrame(np.random.rand(12).reshape(3,4)) 
# spline(dfTest.iloc[0],x,xnew) 
dfBigger = dfTest.apply(lambda row : spline(row, x, xnew), axis=1) 
print(dfBigger) 

利回り

 0  1  2  3  4  5 
0 0.696 0.286 0.161 0.227 0.388 0.551 
1 0.719 0.423 0.630 0.981 1.119 0.685 
2 0.481 0.392 0.333 0.343 0.462 0.729 
+0

すばらしい答え、ありがとう。私は実際にnumpyの配列を与えられたときに関数の動作を取得しません...私は今、パンダに問題が発生したときにシリーズに改造しようと思います。 – Djiggy

関連する問題