我试图确定为什么我的函数产生的最终值不会被保存回我已经指示构建pandas的新列。我已经确认if ... elif语句是正确的和函数,并且我已经确认正在进行计算并生成正确的数据。我得到了这个结果:
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
5 1.0
6 NaN
7 1.0
8 1.0
9 NaN
10 NaN
11 NaN
Name: Result, dtype: float64
但期望得到
0.036231884058
0.18115942029
0.925
0.9255
0.962820512821
1.0
0.368421052632
1.0
1.0
0.950125944584
1.0
0.950125944584
这是我的代码:
import sys
import numpy as np
import pandas as pd
import scipy.stats
df = pd.DataFrame( {
'Cr': [.1,.5,1,1.002,1.2,2,.79,3,3,4,400,4],
'De': [.1,.2,.36,.47,.5,.16,.006,.07,.107,.6,1.7,2.17]
} );
def Fin_adj(row, field):
if field == 'Cr':
if row[field] <= .7:
Range_eval(row[field],0,.69,.0,.25)
elif row[field] <= .9:
Range_eval(row[field],.7,.89,.25,.5)
elif row[field] <= 1.1:
Range_eval(row[field],.9,1.10,.9,.95)
elif row[field] <= 1.5:
Range_eval(row[field],1.1,1.49,.95,1)
elif row[field] <= 3:
return 1
else:
Range_eval(row[field],3,data[field].max(),.95,1)
def Range_eval (val_in, Oldmin, Oldmax, Newmin, Newmax):
(((val_in - Oldmin) * (Newmax - Newmin)) / (Oldmax - Oldmin)) + Newmin
df['Result'] = df.apply(Fin_adj, args=('Cr',), axis=1)
答案 0 :(得分:0)
您忘记了一些return
语句
def Fin_adj(row, field):
if field == 'Cr':
if row[field] <= .7:
return Range_eval(row[field],0.,.69,.0,.25)
elif row[field] <= .9:
return Range_eval(row[field],.7,.89,.25,.5)
elif row[field] <= 1.1:
return Range_eval(row[field],.9,1.10,.9,.95)
elif row[field] <= 1.5:
return Range_eval(row[field],1.1,1.49,.95,1)
elif row[field] <= 3.:
return 1
else:
return Range_eval(row[field],3.,df[field].max(),.95,1)
def Range_eval (val_in, Oldmin, Oldmax, Newmin, Newmax):
return (((val_in - Oldmin) * (Newmax - Newmin)) / (Oldmax - Oldmin)) + Newmin