简体   繁体   English

如何在DataFrame中使用另一列中的值减去一列中的值?

[英]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.

相关问题 如何将列值从一个 dataframe 提取到另一个? - How do I extract column values from one dataframe to another? 如何将 map 一个 dataframe 中的一列字符串值转换为另一个 dataframe 中的另一列? - How do I map a column of string values in one dataframe to another column in another dataframe? 如果使用熊猫在另一个数据帧中不存在列值,如何将它们从一个数据帧合并到另一个数据帧 - How do I merge column values from one dataframe to another if they are not present in another using pandas 如何通过在 Python DataFrame 中保持某些列值不变来将数据从一行合并到另一行 - How do I merge data from one row to another by keeping some column values unchanged in Python DataFrame 如何根据公共列将 dataframe 的列值替换为另一个 dataframe 的值? - How do I replace column values of a dataframe with values of another dataframe based on a common column? 如何通过将python中的目标列的多列与另一数据帧中的一列进行匹配来更新数据框中的目标列的某些值? - How do I update some values of my target column in a dataframe by matching its multiple columns with one column in another dataframe in python? 如果数据框中的另一列使用pandas匹配某个值,则从数据框中的列中减去值 - substract values from column in dataframe if another column in dataframe matches some value using pandas 如何从一个数据框中的列中提取特定值并将它们附加到另一个数据框中的列中? - 熊猫 - How do you extract specific values from a column in one dataframe and append them to a column in another dataframe? - Pandas 如何检查一个数据帧中的列值是否可用或不检查另一数据帧的列中的值? - How to check values of column in one dataframe available or not in column of another dataframe? 如何用另一个 dataframe 的值替换 dataframe 列中的一组值? - how do i replace set of values in a dataframe column with values from another dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM