2016-01-09 44 views

回答

1
import pandas as pd 
import numpy as np 

df = pd.DataFrame({'A':[5, 3, 2, 6], 'B':[2, 4, 1, 8], 'C':[1, 5, 0, 7]}) 

Nrows = len(df) 

def run(col): 
     originalValues = list(df[col]) 
     values = list(np.where(np.array(list(df[col])) == 0)[0]) 
     indices2replace = filter(lambda x: x > 0 and x < Nrows, values) 
     for index in indices2replace: 
       originalValues[index] = 0.5 * (originalValues[index+1] + originalValues[index-1]) 
     return originalValues 


newDF = pd.DataFrame(map(lambda x: run(x) , df.columns)).transpose() 
+1

這種方法可以插入任何通過編輯運行函數 – ranlot

相關問題