pandas 从 DataFrame 中减去一个系列,同时保持 DataFrame 结构完整
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20531990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Subtract a Series from a DataFrame while keeping the DataFrame struct intact
提问by jmatos
How can I subtract a Series from a DataFrame, while keeping the DataFrame struct intact?
如何从 DataFrame 中减去一个系列,同时保持 DataFrame 结构完整?
df = pd.DataFrame(np.zeros((5,3)))
s = pd.Series(np.ones(5))
df - s
0 1 2 3 4
0 -1 -1 -1 NaN NaN
1 -1 -1 -1 NaN NaN
2 -1 -1 -1 NaN NaN
3 -1 -1 -1 NaN NaN
4 -1 -1 -1 NaN NaN
What I would like to have is the equivalent of subtracting a scalar from the DataFrame
我想要的是相当于从 DataFrame 中减去一个标量
df - 1
0 1 2
0 -1 -1 -1
1 -1 -1 -1
2 -1 -1 -1
3 -1 -1 -1
4 -1 -1 -1
回答by DSM
Maybe:
也许:
>>> df = pd.DataFrame(np.zeros((5,3)))
>>> s = pd.Series(np.ones(5))
>>> df.sub(s,axis=0)
0 1 2
0 -1 -1 -1
1 -1 -1 -1
2 -1 -1 -1
3 -1 -1 -1
4 -1 -1 -1
[5 rows x 3 columns]
or, for a more interesting example:
或者,举一个更有趣的例子:
>>> s = pd.Series(np.arange(5))
>>> df.sub(s,axis=0)
0 1 2
0 0 0 0
1 -1 -1 -1
2 -2 -2 -2
3 -3 -3 -3
4 -4 -4 -4
[5 rows x 3 columns]
回答by nikeros
If a1 is a dataframe made of n columns and a2 is a another dataframe made by just 1 column, you can subtract a2 from eachcolumn of a1 using numpy
如果 a1 是由 n 列组成的数据帧,而 a2 是仅由 1 列组成的另一个数据帧,则可以使用 numpy从a1 的每一列中减去 a2
np.subtract(a1, a2)
You can achieve the same result if a2 is a Series making sure to transform to DataFrame
如果 a2 是确保转换为 DataFrame 的系列,您可以获得相同的结果
np.subtract(a1, a2.to_frame())
I guess that, before computing this operation, you need to make sure the indices in the two dataframes are coherent/overlapping. As a matter of fact, the above operations will work if a1 and a2 have the same number of rows and different indices. You can try
我猜想,在计算此操作之前,您需要确保两个数据帧中的索引是一致/重叠的。事实上,如果 a1 和 a2 具有相同的行数和不同的索引,则上述操作将起作用。你可以试试
a1 = pd.DataFrame([[1, 2], [3, 4]], columns=['a','b'])
a2 = pd.DataFrame([[1], [2]], columns=['c'])
np.subtract(a1, a2)
and
和
a1 = pd.DataFrame([[1, 2], [3, 4]], columns=['a','b'])
a2 = pd.DataFrame([[1], [2]], columns=['c'], index=[3,4])
np.subtract(a1,a2)
will give you the same result.
会给你同样的结果。
For this reason, to make sure the two DataFrames are coherent, you could preprocess using something like:
出于这个原因,为了确保两个 DataFrames 是一致的,您可以使用以下内容进行预处理:
def align_dataframes(df1, df2):
r = pd.concat([df1, df2], axis=1, join_axes=[df1.index])
return r.loc[:,df1.columns], r.loc[:,df2.columns]

