I have a dataset like this:
Build_year Max_cnt_year b1920 b1945 b1975 b1995
NaN 120 120 35 45 70
0 67 35 67 21 34
1921 145 39 67 22 145
...
Desired output:
Build_year Max_cnt_year b1920 b1945 b1975 b1995 year_build1
NaN 120 120 35 45 70 1920
0 67 35 67 21 34 1945
1921 145 39 67 22 145 1921
...
I want to compare the max_cnt_year against the values of b1920, b1945, b1975, b1995 and want to assign the values accordingly if it matches to that year ,conditional on Build_year>1500
I am trying this code unsuccessfully:
def mapper(item):
max_val = df_all['max_cnt_year']
comp_val=df_all['build_year']
for comp in comp_val:
if comp<1500 or comp is None:
if max_val==df_all['b1920']:
return 1920
elif max_val==df_all['b1945']:
return 1945
elif max_val==df_all['b1970']:
return 1970
elif max_val==df_all['b1995']:
return 1995
else: return 2005
else: return comp_val
df_all['build_year1'] = map(mapper, df_all)
I have modified the data a bit, to replicate the problem. Actual dataset looks like:
max_cnt_year build_year build_count_before_1920 build_count_1921-1945 \
0 246.0 NaN 1.0 0.0
1 304.0 NaN 0.0 0.0
2 108.0 NaN 0.0 52.0
3 278.0 NaN 23.0 181.0
4 86.0 1945 14.0 45.0
build_count_1946-1970 build_count_1971-1995
0 246.0 63.0
1 304.0 21.0
2 44.0 108.0
3 278.0 131.0
4 86.0 8.0
lambdause itsxargument? But why do you want a hugelambdalike that? Why not just write a properdeffunction?