2 回答
TA贡献1824条经验 获得超5个赞
您可以通过将数组转换为以日期为索引的 pandas 数据框并使用add:
import numpy as np
import pandas as pd
a1 = np.array( [['2019-12-01', '0.03555', '0.03', '0.03', '0.03'],
['2019-12-02', '0.03', '0.03', '1', '0.03']] )
a2 = np.array([['2019-12-01', '1', '1', '1', '1'],
['2019-12-02', '1', '1', '1', '20']])
# convert to dataframe and set the date as the index
# also convert to floats:
df1 = pd.DataFrame(a1).set_index(0).astype(float)
df2 = pd.DataFrame(a2).set_index(0).astype(float)
df1.add(df2, fill_value = 0)
然后,您可以通过转换回字符串、重置索引并获取值来将其作为原始格式的 numpy 数组取回:
df1.add(df2, fill_value = 0).astype(str).reset_index().values
TA贡献1851条经验 获得超3个赞
这对我有用,它不需要熊猫:
import numpy as np
a1 = [
np.array(['2019-12-01', '0.03555', '0.03', '0.03', '0.03']),
np.array(['2019-12-02', '0.03', '0.03', '1', '0.03'])
]
a2 = [
np.array(['2019-12-01', '1', '1', '1', '1']),
np.array(['2019-12-02', '1', '1', '1', '20'])
]
def array_adder(A, B):
C = []
for outer, outer2 in zip(A, B):
temp = []
if len(outer) == len(outer2):
for inner, inner2 in zip(outer, outer2):
if len(temp) < 1:
temp.append(inner)
else:
temp.append(float(inner) + float(inner2))
C.append(temp)
else:
print('Arrays must have the same length..')
break
return C
print(array_adder(a1, a2))
添加回答
举报