You can get the maximum like this:
>>> import pandas as pd
>>> df = pd.DataFrame({"A": [1,2,3], "B": [-2, 8, 1]})
>>> df
A B
0 1 -2
1 2 8
2 3 1
>>> df[["A", "B"]]
A B
0 1 -2
1 2 8
2 3 1
>>> df[["A", "B"]].max(axis=1)
0 1
1 8
2 3
and so:
>>> df["C"] = df[["A", "B"]].max(axis=1)
>>> df
A B C
0 1 -2 1
1 2 8 8
2 3 1 3
If you know that "A" and "B" are the only columns, you could even get away with
>>> df["C"] = df.max(axis=1)
And you could use .apply(max, axis=1) too, I guess.
You can get the maximum like this:
>>> import pandas as pd
>>> df = pd.DataFrame({"A": [1,2,3], "B": [-2, 8, 1]})
>>> df
A B
0 1 -2
1 2 8
2 3 1
>>> df[["A", "B"]]
A B
0 1 -2
1 2 8
2 3 1
>>> df[["A", "B"]].max(axis=1)
0 1
1 8
2 3
and so:
>>> df["C"] = df[["A", "B"]].max(axis=1)
>>> df
A B C
0 1 -2 1
1 2 8 8
2 3 1 3
If you know that "A" and "B" are the only columns, you could even get away with
>>> df["C"] = df.max(axis=1)
And you could use .apply(max, axis=1) too, I guess.
@DSM's answer is perfectly fine in almost any normal scenario. But if you're the type of programmer who wants to go a little deeper than the surface level, you might be interested to know that it is a little faster to call numpy functions on the underlying .to_numpy() (or .values for <0.24) array instead of directly calling the (cythonized) functions defined on the DataFrame/Series objects.
For example, you can use ndarray.max() along the first axis.
# Data borrowed from @DSM's post.
df = pd.DataFrame({"A": [1,2,3], "B": [-2, 8, 1]})
df
A B
0 1 -2
1 2 8
2 3 1
df['C'] = df[['A', 'B']].values.max(1)
# Or, assuming "A" and "B" are the only columns,
# df['C'] = df.values.max(1)
df
A B C
0 1 -2 1
1 2 8 8
2 3 1 3
If your data has NaNs, you will need numpy.nanmax:
df['C'] = np.nanmax(df.values, axis=1)
df
A B C
0 1 -2 1
1 2 8 8
2 3 1 3
You can also use numpy.maximum.reduce. numpy.maximum is a ufunc (Universal Function), and every ufunc has a reduce:
df['C'] = np.maximum.reduce(df['A', 'B']].values, axis=1)
# df['C'] = np.maximum.reduce(df[['A', 'B']], axis=1)
# df['C'] = np.maximum.reduce(df, axis=1)
df
A B C
0 1 -2 1
1 2 8 8
2 3 1 3

np.maximum.reduce and np.max appear to be more or less the same (for most normal sized DataFrames)—and happen to be a shade faster than DataFrame.max. I imagine this difference roughly remains constant, and is due to internal overhead (indexing alignment, handling NaNs, etc).
The graph was generated using perfplot. Benchmarking code, for reference:
import pandas as pd
import perfplot
np.random.seed(0)
df_ = pd.DataFrame(np.random.randn(5, 1000))
perfplot.show(
setup=lambda n: pd.concat([df_] * n, ignore_index=True),
kernels=[
lambda df: df.assign(new=df.max(axis=1)),
lambda df: df.assign(new=df.values.max(1)),
lambda df: df.assign(new=np.nanmax(df.values, axis=1)),
lambda df: df.assign(new=np.maximum.reduce(df.values, axis=1)),
],
labels=['df.max', 'np.max', 'np.maximum.reduce', 'np.nanmax'],
n_range=[2**k for k in range(0, 15)],
xlabel='N (* len(df))',
logx=True,
logy=True)
pandas - How do I find pairwise maximum of multiple rows in a column using python? - Data Science Stack Exchange
Python Pandas add column for row-wise max value of selected columns - Stack Overflow
Finding max of the difference between two columns in dataframe
Pandas: Get the max value of a group ONLY if the value satisfies given conditions
Hi folks, I would like to find the index of the row that contains the maximum value between the two columns. This is what I have:
df = pd.DataFrame({"2021":[5, 8, 12, 9, 5, 3],
"2022":[11, 4, 8, 7, 3, 2]})
df1 = df.iloc[df[(df["2021"] - df["2022"]) == (df["2021"] - df["2022"]).max()].index]It looks kind of messy. I am not sure if this is the best way to get the answer. Do you have simpler recommendations? Please advise. Thanks.
This would be another approach a little bit shorter:
df.assign(resultado = lambda x: x.rolling(2).max())
EDIT:
For your comment try:
def idx(x):
return x.index.values[np.argmax(x.values)]
df.rolling(2).agg(['max', idx])
will return both, the pairwise maximum and the index that corresponds to that value.
You can create a new column like the one you have but "shifted" one position down, and the compute the maximum of these two columns:
import pandas as pd
import numpy as np
data = np.random.randint(0, 50, size=20)
df = pd.DataFrame(data, columns=['values'])
df['prev'] = df['values'].shift(1)
df['max'] = df[['values', 'prev']].max(axis=1)
The result is
+----+----------+--------+-------+
| | values | prev | max |
|----+----------+--------+-------|
| 0 | 17 | nan | 17 |
| 1 | 32 | 17 | 32 |
| 2 | 3 | 32 | 32 |
| 3 | 4 | 3 | 4 |
| 4 | 4 | 4 | 4 |
| 5 | 17 | 4 | 17 |
| 6 | 12 | 17 | 17 |
...
You can then remove the first row if you don't need it.
>>> frame['HighScore'] = frame[['test1','test2','test3']].max(axis=1)
>>> frame
name test1 test2 test3 HighScore
0 bill 85 35 51 85
1 joe 75 45 61 75
2 steve 85 83 45 85
>>> frame['HighScore'] = frame[['test1','test2','test3']].apply(max, axis=1)
>>> frame
name test1 test2 test3 HighScore
0 bill 85 35 51 85
1 joe 75 45 61 75
2 steve 85 83 45 85