[英]Select row in Pandas dataframe where a column value is max based on a function of two columns
I have the following dataframe.我有以下数据框。
data = {'Name': ["Babu", "Shyam", "Raju", "Anuradha", "Kabira"],
'Age': [60, 35, 32, 31, 37],
'Income': [20000, 10000, 8000, 12000, 5000],
'Stupidity Level': [80, 40, 60, 20, 70],
'Expenses': [15000,8000,7000,9000,4000]
}
index = ["Paresh Rawal", "Suniel Shetty", "Akshay Kumar","Tabu", "Gulshan Grover"]
df = pd.DataFrame(data, index)
I am trying to find out a row (person) who saves maximum amount every month.我试图找出每个月节省最大金额的行(人)。
savings = df["Income"] - df["Expenses"]
savings.max()
5000
In this case, it should return the first row whose savings is maximum (5000).在这种情况下,它应该返回节省最大 (5000) 的第一行。 But I am trying to this without actually creating a new column for savings.但我正在尝试这样做,但实际上并没有创建一个新的储蓄列。 So want to do something like所以想做类似的事情
df[savings.max()] # should return the row with maximum savings.
df[(df["Income"] - df["Expenses"]).max()]
But of course, none of this isn't working.但是,当然,这些都行不通。 Not sure of the correct synatax.不确定正确的语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.