I want to store the maximum values in the array below into a dataframe's column
# dataset
import pandas as pd
data = {'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair','Watch'],
'price': [1200, 150, 300, 450, 200,90],
'class':['good','bad','bad','good','bad','good']
}
df = pd.DataFrame(data)
print(df)
#####################################################
arr = [[0.11085975, 0.88914025],
[0.69934523, 0.30065477],
[0.6325009 , 0.36749908],
[0.8115895 , 0.18841055],
[0.8882814 , 0.11171862],
[0.891402 , 0.10859799]]
My attempt
df['max_score'] = np.max(arr) # this only returns `0.891402`
Kindly share your code, thanks in advance.
CodePudding user response:
Unlike pandas, that defaults to axis=0
for most 2D operations, numpy defaults to axis=None
, creating a single scalar. See numpy.ndarray.max
Use axis=1
:
df['max_score'] = np.max(arr, axis=1)
Output:
product_name price class max_score
0 laptop 1200 good 0.889140
1 printer 150 bad 0.699345
2 tablet 300 bad 0.632501
3 desk 450 good 0.811589
4 chair 200 bad 0.888281
5 Watch 90 good 0.891402