'Trying to compare row values, and print a message in anew column for the row that has the highest
If you look on the far right i was able to do Kinda of what i wanted, but its only comparing European sales to north American sales.
However, I want to compare European sales against the sales in the other columns too (compare it to multiple columns). How can i go about doing this ? not just compare against NA_Sales
Essentially, i want to compare the sales of one columns against them all, and which every has the higher i want it to say that game X sold more in X continent. Thanks
this is the code is used for EU sales against Na sales
def desc(row):
if row['EU_Sales'] > row['NA_Sales']:
return '{} sold more in europe'.format(row['Name'])
else:
return 'nothing'
df1['status'] = df1.apply(desc, axis = 1)
Thanks
Also, is there a better way to word this question ? thanks.
Solution 1:[1]
IIUC, you can try np.where
import numpy as np
df1['status'] = np.where(df1['EU_Sales'] > df1['NA_Sales'], df1['Name']+' sold more in europe', 'nothing')
You can use max(axis=1).idxmax() to get the coresponding column name of max value
df1['status'] = df1['Name'] + ' sold more in ' + df1[['EU_Sales', 'NA_Sales', 'JP_Sales']].idxmax(axis=1).str.split('_').str[0]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
