【发布时间】:2017-01-25 21:40:14
【问题描述】:
我有一个数据框,我希望将单元格的浮点值除以它所在行的总和。为此,我使用了一个 numpy 公式。因此,这会给我该行的该单元格的权重。我有这个数据框df1:
AA AB AC AD
2005-01-02 23:55:00 "EQUITY" "EQUITY" "EQUITY" "EQUITY"
2005-01-03 00:00:00 32.32 19.5299 32.32 31.0455
2005-01-04 00:00:00 31.9075 19.4487 31.9075 30.3755
2005-01-05 00:00:00 31.6151 19.5799 31.6151 29.971
2005-01-06 00:00:00 31.1426 19.7174 31.1426 29.9647
到目前为止,我已经尝试了以下方法:
import numpy as np
def foo_bar(x):
if type(x) is not str:
return x / np.sum(df1, axis=1)
else:
return
df = df_numeric.apply(np.vectorize(foo_bar))
我的公式的总和部分似乎没有正确编写,因为我收到以下错误:
AA AB AC AD
ValueError: ('setting an array element with a sequence.', 'occurred at index AA')
【问题讨论】:
标签: python pandas numpy dataframe