Be the following python pandas DataFrame (df):
age | money | time |
---|---|---|
10 | 300 | 10 |
8 | 200 | 20 |
20 | 1800 | 80 |
15 | 200 | 50 |
I want to extract the minimum value for each column:
age | money | time |
---|---|---|
8 | 200 | 10 |
Given this other new dataframe (new_df):
age | money | time |
---|---|---|
30 | -100 | 15 |
10 | 100 | 50 |
-2 | 1800 | -20 |
18 | -50 | 52 |
All values that are less than the minima of each column are set to the minimum value of the previous dataframe.
age | money | time |
---|---|---|
30 | 200 | 15 |
10 | 200 | 50 |
8 | 1800 | 10 |
18 | 200 | 52 |
CodePudding user response:
You can use min
to get the min of df
, then clip
to clip the values of new_df
:
out = new_df.clip(lower=df.min(), axis=1)
Output:
age money time
0 30 200 15
1 10 200 50
2 8 1800 10
3 18 200 52
Restricting to a subset of columns:
cols = ['age', 'time']
out = new_df[cols].clip(lower=df.min(), axis=1).combine_first(new_df)
output:
age money time
0 30 -100 15
1 10 100 50
2 8 1800 10
3 18 -50 52