series.sum() 给出 NaN 结果

时间:2021-02-08 00:59:40

标签: python python-3.x pandas dataframe

下表显示了两个用户之间的交易,我需要找到每个用户的净资产变化。

enter image description here

我使用了以下代码,但它给用户 4 和 5 的结果不正确

prior_fish =  [1.00000000e+01 1.20679264e+01 1.45634848e+01 1.75751062e+01
 2.12095089e+01 2.55954792e+01 3.08884360e+01 3.72759372e+01
 4.49843267e+01 5.42867544e+01 6.55128557e+01 7.90604321e+01
 9.54095476e+01 1.15139540e+02 1.38949549e+02 1.67683294e+02
 2.02358965e+02 2.44205309e+02 2.94705170e+02 3.55648031e+02
 4.29193426e+02 5.17947468e+02 6.25055193e+02 7.54312006e+02
 9.10298178e+02 1.09854114e+03 1.32571137e+03 1.59985872e+03
 1.93069773e+03 2.32995181e+03 2.81176870e+03 3.39322177e+03
 4.09491506e+03 4.94171336e+03 5.96362332e+03 7.19685673e+03
 8.68511374e+03 1.04811313e+04 1.26485522e+04 1.52641797e+04
 1.84206997e+04 2.22299648e+04 2.68269580e+04 3.23745754e+04
 3.90693994e+04 4.71486636e+04 5.68986603e+04 6.86648845e+04
 8.28642773e+04 1.00000000e+05]
Traceback (most recent call last):
  File "final_Jacobian_WITH_PRIOR_ONLY_on_SUPER_FISH_GCsp_dev_into_GCsp_plus_3x2pt_dev.py", line 119, in <module>
    np.stack([prior_fish.T, np.arange(5)])
  File "<__array_function__ internals>", line 6, in stack
  File "/opt/intel/intelpython3/lib/python3.7/site-packages/numpy/core/shape_base.py", line 426, in stack
    raise ValueError('all input arrays must have the same shape')
ValueError: all input arrays must have the same shape

enter image description here

1 个答案:

答案 0 :(得分:1)

已编辑:

您可以使用 pandas 的 DataFrame.subtract 减去数据帧,如果两个数据帧上没有相同的索引,您可以指定要填充的值

credit = ts.groupby('sender').sum()
debit = ts.groupby('receiver').sum()
net_change = debit.subtract(credit, fill_value=0)
net_change

onEachFeature option

相关问题