简体   繁体   English

Pandas 中有没有办法减去同一列中同名的两个值?

[英]Is there a way in Pandas to subtract two values that are in the same column that have the same name?

Here is a snippet of a dataframe I'm trying to analyze.这是我正在尝试分析的 dataframe 的片段。 What I want to do is simply subtract FP_FLOW FORMATTED_ENTRY values from D8_FLOW FORMATTED_ENTRY values only if the X_LOT_NAME is the same.我要做的只是仅当 X_LOT_NAME 相同时才从 D8_FLOW FORMATTED_ENTRY 值中减去 FP_FLOW FORMATTED_ENTRY 值。 For example, in the X_LOT_NAME column you can see MPACZX2.例如,在 X_LOT_NAME 列中,您可以看到 MPACZX2。 The D8_FLOW FORMATTED_ENTRY is 12.3%. D8_FLOW FORMATTED_ENTRY 为 12.3%。 The FP_FLOW FORMATTED_ENTRY value is 7.8%. FP_FLOW FORMATTED_ENTRY 值为 7.8%。 The difference between the two would be 4.5%.两者之间的差异为 4.5%。 I want to apply this logic across the whole data set我想在整个数据集中应用这个逻辑

在此处输入图像描述

Is this what you are looking for?这是你想要的?

df.groupby(['x_lot'])['value'].diff()

0     NaN
1     NaN
2    -5.0
3     8.0
4    -3.0
5     NaN
6   -10.0
Name: value, dtype: float64

This is the data i used to get the above results这是我用来获得上述结果的数据

    x_lot   type    value
0   mpaczw1 fp  21
1   mpaczw2 d8  12
2   mpaczw2 fp  7
3   mpaczw2 d8  15
4   mpaczw2 fp  12
5   mpaczw3 d8  21
6   mpaczw3 fp  11

it is advisable to first convert your data into a format where the values to be added / subtracted are in the same row, and after that subtract / add the corresponding oclumns.建议首先将您的数据转换为要添加/减去的值在同一行中的格式,然后减去/添加相应的 oclumns。 You can do this using pd.pivot-table .您可以使用pd.pivot-table执行此操作。 The below example will demonstrate this using a sample dataframe similar to what you've shared:下面的示例将使用与您共享的示例 dataframe 类似的示例演示这一点:

wanted_data

    X_LOT_NAME  SPEC_TYPE   FORMATTED_ENTRY
0   a   FP_FLOW     1
1   a   D8_FLOW     2
2   c   FP_FLOW     3
3   c   D8_FLOW     4

pivot_data  = pd.pivot_table(wanted_data,values='FORMATTED_ENTRY',index='X_LOT_NAME',columns='SPEC_TYPE')
pivot_data
SPEC_TYPE   D8_FLOW     FP_FLOW
X_LOT_NAME      
a   2   1
c   4   3

After this step, the resultant pivot_data contains the same data, but the columns are D8_FLOW and FP_FLOW , with X_LOT_NAME as the index.在这一步之后,生成的pivot_data包含相同的数据,但列是D8_FLOWFP_FLOW ,以X_LOT_NAME作为索引。 Now you can get the intended value in a new column using:现在,您可以使用以下方法在新列中获取预期值:

pivot_data['DIFF'] =  pivot_data['D8_FLOW'] - pivot_data['FP_FLOW'] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Select 两个 pandas 列,列名中的数字相同 - Select two pandas columns that have the same digit in the column name 有没有办法让 map 两个 pandas.series 具有相同的值? - Is there a way to map two pandas.series to have the same values? 根据相同的列名称值在熊猫中合并两个数据框 - Merge two dataframes in pandas based on the same column name values 如果其他col具有相同的值,则减去两个df col - Subtract two df col if other col have same values 如果两列中的值相同,则合并熊猫中的单元格 - Merge cells in pandas if values in two column is same 减去两个同名不同索引的数据帧 - Subtract two dataframe with the same name different index 如何按列名过滤值,然后将具有相同值的行提取到另一个CSV文件? Python /熊猫 - How to filter values by Column Name and then extract the rows that have the same value to another CSV file? Python/Pandas 用相同的值填充两个 pandas 列值之间的值 - Fill the values between two pandas column values with same values 如何在具有相似值(但不相同)的公共列上合并 Pandas 中的两个数据框? - How do I merge two data frames in pandas on a common column which have similar values (but not the same)? Pandas:如果两行或多行在特定列中具有相同的值,则获取其计数并添加到下一行 - Pandas: If two or more rows have same values in particular column then get its count and add to next row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM