如何编写python代码来计算新列的值--"UEC_saving“除以”UEC“列中的行数偏移量。表格式为pandas数据帧。
"Offset_rows“列中的正数表示将行向下移动,反之亦然。
例如,索引0的"UEC_saving“是8.6-7.2,索引2的是0.2-7.0
"UEC_saving“的输出应该如下所示:
Product_Class UEC Offset_rows UEC_saving
0 PC1 8.6 1 1.4
1 PC1 7.2 0 0.0
2 PC1 0.2 -1 -7.0
3 PC2 18.8 2 2.2
4 PC2 10.0 1 1.4
5 PC2 8.6 0 0.0
6 PC2 0.3 -1 -8.3发布于 2019-09-17 04:26:21
您可以执行以下操作:
for i,row in df.iterrows():
df.at[i,'UEC_saving'] = row['UEC'] - df.loc[i+row['Offset_rows']]['UEC']
df
UEC Offset_rows UEC_saving
0 8.6 1 1.4
1 7.2 0 0.0
2 0.2 -1 -7.0
3 18.8 2 10.2
4 10.0 1 1.4
5 8.6 0 0.0
6 0.3 -1 -8.3这是除了索引3之外的所有你想要的答案,如果有拼写错误,请让我知道,或者请在你的问题中进一步解释到底发生了什么
https://stackoverflow.com/questions/57963699
复制相似问题