python - Return max value of each row of a group of columns -
i have table of on 10,000 rows , on 400 columns. columns containing @ least string 'xyz', need find max value of each row (within these 'xyz' columns), , create 2 new columns.
the 1st new column contain max value of each row of these 'xyz' columns.
the 2nd new column contain column name max value retrieved. i'm stuck @ creating 2nd column. i've tried stuff doesn't work like;
match = df[compcol].isin[speclist].all(axis=1)
how should approach 2nd column?
does work you?
import pandas pd df = pd.dataframe([(1,2,3,4),(2,1,1,4)], columns = ['xyz1','xyz2','xyz3','abc']) cols = [k k in df.columns if 'xyz' in k] df['maxval'] = df[cols].apply(lambda s: max(zip(s, s.keys()))[0],1) df['maxcol'] = df[cols].apply(lambda s: max(zip(s, s.keys()))[1],1) df out[753]: xyz1 xyz2 xyz3 abc maxval maxcol 0 1 2 3 4 3 xyz3 1 2 1 1 4 2 xyz1
Comments
Post a Comment