2017-04-06 117 views
0

我想創建一個使用數據框的列作爲輸入的函數。這部分輸入將是一系列的。熊貓將函數應用於數據框中的數據框索引系列

因此,這將功能類似下面

def myFunc(input1, input2, s_input): 
    for s in s_input['c':'d'].index: 
     print s + str(s_input[s]) +str(input1) 
    for s in s_input['e':].index: 
     print s + str(s_input[s]) +str(input2) 

s = pd.Series({'a':1,'b':4,'c':7,'d':11,'e':15,'f':22}) 

input1=' input1' 
input2 =' input2' 

myFunc(input1,input2,s) 

c7 input1 
d11 input1 
e15 input2 
f22 input2 

我想如果你想申請的功能,逐行這裏,在使用axis = 1將其應用到數據幀像

df = pd.DataFrame({'a':[1,2,3],'b':[4,5,6],'c':[7,8,9],'d':[11,12,13], 'e':[11,23,33], 'f':[75,44,55]})  

df.apply(lambda x: myFunc(df.a,df.b, df.loc[x.index,'c':])) 
+3

這裏的問題究竟是什麼? – Vaishali

回答

1

apply

df.apply(lambda x: myFunc(x.a, x.b, x.loc['c':]), axis=1) 

# gets printed... 
c71 
d111 
e114 
f754 
c82 
d122 
e235 
f445 
c93 
d133 
e336 
f556 

但是,您通常會僱用apply返回值。目前,您的功能不會返回任何內容。

+0

我知道我很親密。現在我想我明白拉姆達,不知道爲什麼我以前沒有。 –