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