Issue
I have a pandas dataframe with this structure:
df_test
V | A | B | C | D
-10 | nan | nan | nan | nan
-9.9 | 10 | 1 | -2200 | 100
-9.8 | 11 | 2 | -2211 | 1
I would like to add a new column minimum_difference
containing the column name with the smallest absolute value for that specific row (ignoring V
), like this:
V | A | B | C | D | minimum_difference
-10 | nan | nan | nan | nan | nan
-9.9 | 10 | 1 | -2200 | 100 | B
-9.8 | 11 | 2 | -2211 | 1 | D
Solution
You can use df.drop
to skip the V
column, and df.abs
to convert the dataframe's values to their absolute equivalents, and use df.idxmin
with axis=1
to find the index (which will be a column name) of the smallest (absolute) value:
df_test['minimum_difference'] = df_test.drop('V', axis=1).abs().idxmin(axis=1)
Output:
>>> df
V A B C D minimum_difference
0 -10.0 NaN NaN NaN NaN NaN
1 -9.9 10.0 1.0 -2200.0 100.0 B
2 -9.8 11.0 2.0 -2211.0 1.0 D
Answered By - richardec
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.