从另一个列中减去一个数据框(第一列Pandas除外) [英] Subtract one dataframe from another excluding the first column Pandas

查看:265
本文介绍了从另一个列中减去一个数据框(第一列Pandas除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须具有相同列的数据框.我的任务应该是从df_nap中减去df_tot,而无需触摸第一列('A'). 最简单的解决方案是什么?

I have to dataframes with the same columns. My task should be to subtract the df_tot from df_nap without touching the first column ('A'). What is the easiest solution for it?

谢谢!

import numpy as np
import pandas as pd

df_tot = pd.DataFrame(np.random.randint(10, size=(3,4)), columns=list('ABCD'))
df_nap = pd.DataFrame(np.random.randint(10, size=(3,4)), columns=list('ABCD'))

推荐答案

简单地减去整个DataFrame,然后将所需的值重新分配给Wavelength列.

Simply subtract the entire DataFrames, then reassign the desired values to the Wavelength column.

result = df_tot - df_nap
result['Wavelength'] = df_tot['Wavelength']

例如,

import numpy as np
import pandas as pd

df_tot = pd.DataFrame(np.random.randint(10, size=(3,4)), columns=list('ABCD'))
df_nap = pd.DataFrame(np.random.randint(10, size=(3,4)), columns=list('ABCD'))
# df_tot['A'] = df_nap['A']   # using column A as the "Wavelength" column

result = df_tot - df_nap
result['A'] = df_tot['A']


或者,如果波长"列不是数字,则可以 减去除波长以外的所有内容,然后重新分配该列:


Alternatively, or if Wavelength column were not numeric, you could subtract everything except the Wavelength, then reassign that column:

result = df_tot.drop('Wavelength', axis=1) - df_nap.drop('Wavelength', axis=1)
result['Wavelength'] = df_tot['Wavelength']

这篇关于从另一个列中减去一个数据框(第一列Pandas除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆