Given a DataFrame
a b c d
1 5 5 5 5
2 5 5 5 5
3 5 5 5 5
I would like to add more columns on the DataFrame based on the existing ones but using some logic that can't fit in a lambda. The desired result should look something like this:
a a_added c c_added d d_added
1 5 'good' 5 'good' 5 'bad'
2 5 'bad' 5 'good' 5 'bad'
3 5 'good' 5 'good' 5 'bad'
After seeing this answer, my idea was to use DataFrame.apply()
on each row and after that Series.apply()
on each value but I don't know exactly how to chain the calls and what exactly to return such that I return a new column name from the Series's apply function. After that I think I need to combine those two DataFrames with DataFrame.join()
. I really need to use Series.apply()
because I have to compute each value with some custom logic.
EDIT: I have a map of of thresholds where the keys correspond to the column names in my DataFrame and the values are warning/critical thresholds plus an operation that says how the current value should be compared against the threshold:
thresholds = {
'a': {'warning': 90, 'critical': 98, operation: 'lt'},
'b': {'warning': 10, 'critical': 15, operation: 'gt'},
'c': {'warning': 5, 'critical': 9, operation: 'le'}
}
EDIT2: Using the following input with the thresholds above:
a b c
1 89 0 4
2 91 9 10
3 99 17 5
will get as result:
a a_r b b_r c c_r
1 89 good 0 good 4 good
2 91 warn 9 warn 10 crit
3 99 crit 17 good 5 warn
Therefore for each value depending on the column name I have to apply the corresponding threshold from the map.