[英]How do I Substract values in one column with the values of another in a DataFrame?
I have the following dataframe: 我有以下数据帧:
I want to see which country has the biggest difference between the column "Gold" and "Gold 1". 我想看看哪个国家在“黄金”和“黄金1”栏目之间存在最大差异。 The index currently is the countries.
该指数目前是各国。
As an example, with Afghanistan, it would be 0 - 0 = 0. I do this with every country and than the highest number in that list is my response. 例如,对于阿富汗,它将是0-0 = 0.我对每个国家这样做,而且该列表中的最高数字是我的回答。 That's how I figured I want to do it.
这就是我想要做的事情。
Does anyone know how I can do that? 有谁知道我怎么做到这一点? Or is there a built-in function that can calculate that?
或者是否有内置函数可以计算出来?
您可以使用内置向量减法减去这两列:
df1['Gold'] - df2['Gold 1']
The country with biggest difference is 这个国家最大的区别是
df.Gold.sub(df['Gold 1']).idxmax()
The biggest absolute difference 最大的绝对差异
df.Gold.sub(df['Gold 1']).abs().idxmax()
You can also sort this by the difference 您也可以通过差异对此进行排序
df.loc[df.Gold.sub(df['Gold 1']).sort_values().index]
Or the absolute differences 或绝对的差异
df.loc[df.Gold.sub(df['Gold 1']).abs().sort_values().index]
you can try out the code below: 你可以试试下面的代码:
import pandas as pd
df = pd.DataFrame([['Afgh',0,0],['Agnt',18,0]], columns = ['Country','Gold','Gold1'])
df['GoldDiff'] = df['Gold'] - df['Gold1']
df.sort_values(by = 'GoldDiff', ascending = False)
df
is just a test dataframe based on yours above. df
只是一个基于你的测试数据帧。 df['GoldDiff']
creates a new column to store differences. df['GoldDiff']
创建一个新列来存储差异。
Then you can simply sort the values by using the sort_values
function from pandas. 然后,您可以使用pandas中的
sort_values
函数对值进行简单排序。 You can also add the option inplace = True
if you want to modify your dataframe as the sorted one. 如果要将数据框修改为已排序的数据框,也可以添加
inplace = True
选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.