Given that, I have a dataframe as below:
import pandas as pd
import numpy as np
dict = {
"A": [[1,2,3,4],[3],[2,8,4],[5,8]]
}
dt = pd.DataFrame(dict)
I wish to have the Maximum and minimum of each row in column B. My favorite output is:
A B
0 [1, 2, 3, 4] [1,4]
1 [3] [3,3]
2 [2, 8, 4] [2,8]
3 [5, 8] [5,8]
What I already tried is the below code which does not work:
dt["B"] =[np.min(dt.A), np.max(dt.A)]
Like this:
In [1592]: dt['B'] = dt.A.apply(lambda x: [min(x), max(x)])
In [1593]: dt
Out[1593]:
A B
0 [1, 2, 3, 4] [1, 4]
1 [3] [3, 3]
2 [2, 8, 4] [2, 8]
3 [5, 8] [5, 8]
As suggested by @Ch3steR, using map
since it's faster:
dt['B'] = dt.A.map(lambda x: [min(x), max(x)])
You can create DataFrame
, then minimal and maximal values by DataFrame.agg
, convert to lists and assign back if requirement is no loops (Apply
are loops under the hood):
df = pd.DataFrame(dt.A.tolist())
dt['B'] = df.agg(['min','max'], axis=1).astype(int).values.tolist()
print (dt)
A B
0 [1, 2, 3, 4] [1, 4]
1 [3] [3, 3]
2 [2, 8, 4] [2, 8]
3 [5, 8] [5, 8]
If no problem with loops another solution with list comprehension
, it should be faster like apply
, depends of real data:
dt['B'] = [[min(x), max(x)] for x in dt.A]
Just an alternative with explode
:
dt['B'] = (dt['A'].explode().astype(int).groupby(level=0).agg(['min','max'])
.to_numpy().tolist())
print(dt)
A B
0 [1, 2, 3, 4] [1, 4]
1 [3] [3, 3]
2 [2, 8, 4] [2, 8]
3 [5, 8] [5, 8]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With