[英]Pandas: nansum Series to each column of DataFrame
I would like to sum a Series to each of the columns of a DataFrame, having the same behaviour of nansum: if one component is missing, return the other; 我想将一个系列加到一个DataFrame的每一列上,具有相同的nansum行为:如果一个组件丢失,则返回另一个; if both are missing return NaN/0.
如果两者都丢失则返回NaN / 0。
Test case: 测试用例:
import numpy as np
import pandas as pd
df = pd.DataFrame([[np.nan, 1], [2, 2], [3, 4]])
ts = pd.Series([1, np.nan, 4])
Attempt 1 (gives nan when one is missing): 尝试1(当缺少一个时给出nan):
df.add(ts, axis=0)
Out[115]:
0 1
0 NaN 2.0
1 NaN NaN
2 7.0 8.0
Attempt 2 (throws error): 尝试2(抛出错误):
df.add(ts, axis=0, fill_value=0)
NotImplementedError: fill_value 0 not supported.
Expected Output: 预期产出:
0 1
0 1 2
1 2 2
2 7 8
Note: I could do it by saving location of NaNs, doing the sum with NaN filled with 0 and set to NaN when both are NaN, but I am looking for a better solution. 注意:我可以通过保存NaN的位置来做到这一点,用NaN填充为0并且当两者都是NaN时设置为NaN,但我正在寻找更好的解决方案。
fill_value
for anything besides None
is still in the works, but as of now, you'll have to workaround this. 除了
None
之外的任何东西的fill_value
仍在开发中,但截至目前,你必须解决这个问题。 Perform addition, and then compute the mask to reinstate NaN
s where required. 执行添加,然后计算掩码以在需要时恢复
NaN
。
r = df.fillna(0).add(ts.fillna(0), axis=0)
m = df.isnull().__and__(ts.isnull(), axis=0).values
v = r.values
v[m] = np.nan
r[:] = v
r
0 1
0 1.0 2.0
1 2.0 2.0
2 7.0 8.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.