Python: vlookup on complex arrays searching through multiple columns for each row -
i have following problem python code doesn't work. hoping suggestions on why , how resolve.
here's example dataframe:
cust_id max_nibt nibt_0 nibt_1 nibt_10 line_0 line_1 line_10 11 200 -5 200 500 100 200 300 22 300 -10 100 300 100 200 300 33 400 -20 0 400 100 200 300
for in range (0,11): if (df4['nibt_%s' % i] == df4['max_nibt']): df4['model_line'] = df4['line_%s' % i]
the code gives me following error:
valueerror: truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all()
however, when use .any()
, gives me last range assigning model_line = line_10. when use .all()
, answer same cust_ids. thoughts? in advance.
i have guess @ want, not using pd.series
correctly... see here better explanation.
iiuc:
want fill in values line_x
when nibt_x
equals max_nibt
# filter `nibt` columns , find first column equals max nibt_maxes = df.filter(regex='nibt_\d+').eq(df.max_nibt, 0).idxmax(1) # swap out string `nibt` `line` lines = nibt_maxes.replace('nibt', 'line', regex=true) # use `lookup` , assign values df['model'] = df.lookup(lines.index, lines.values) cust_id max_nibt nibt_0 nibt_1 nibt_10 line_0 line_1 line_10 model 0 11 200 -5 200 500 100 200 300 200 1 22 300 -10 100 300 100 200 300 300 2 33 400 -20 0 400 100 200 300 300
Comments
Post a Comment