使用'乘以两个python数组;查找';
我需要将每个“单元格”中的值乘以相应的权重(不包括第一列)。例如:使用'乘以两个python数组;查找';,python,pandas,dataframe,Python,Pandas,Dataframe,我需要将每个“单元格”中的值乘以相应的权重(不包括第一列)。例如: import numpy as np import pandas as pd columns = ['id', 'A', 'B', 'C'] index = np.arange(3) df = pd.DataFrame(np.random.randn(3,4), columns=columns, index=index) weights = {'A': 0.10, 'B': 1.00, 'C': 1.50} 最有效的方法
import numpy as np
import pandas as pd
columns = ['id', 'A', 'B', 'C']
index = np.arange(3)
df = pd.DataFrame(np.random.randn(3,4), columns=columns, index=index)
weights = {'A': 0.10, 'B': 1.00, 'C': 1.50}
最有效的方法是什么,并在新的数据帧中显示结果?设置
df.at[0,'A'] * weights['A']
df.at[0,'B'] * weights['B']
解决方案
df
Out[1013]:
id A B C
0 -0.641314 -0.526509 0.225116 -1.131141
1 0.018321 -0.944734 -0.123334 -0.853356
2 0.703119 0.468857 1.038572 -1.529723
weights
Out[1026]: {'A': 0.1, 'B': 1.0, 'C': 1.5}
W = np.asarray([weights[e] for e in sorted(weights.keys())])
更新
如果需要保持列名的灵活性,我认为更好的方法是将列名和权重保存在两个列表中:
#use a matrix multiplication to apply the weights to each column
df.loc[:,['A','B','C']] *= W
df
Out[1016]:
id A B C
0 -0.641314 -0.052651 0.225116 -1.696712
1 0.018321 -0.094473 -0.123334 -1.280034
2 0.703119 0.046886 1.038572 -2.294584
然后你可以这样做:
columns = sorted(weights.keys())
Out[1072]: ['A', 'B', 'C']
weights = [weights[e] for e in columns]
Out[1074]: [0.1, 1.0, 1.5]
一条龙解决方案:
df.loc[:,columns] *=weights
Out[1067]:
id A B C
0 -0.641314 -0.052651 0.225116 -1.696712
1 0.018321 -0.094473 -0.123334 -1.280034
2 0.703119 0.046886 1.038572 -2.294584
如果你喜欢,这里有一个简单的方法:
df.loc[:,sorted(weights.keys())] *=[weights[e] for e in sorted(weights.keys())]
df
Out[1089]:
id A B C
0 -0.641314 -0.052651 0.225116 -1.696712
1 0.018321 -0.094473 -0.123334 -1.280034
2 0.703119 0.046886 1.038572 -2.294584
或者,如果要替换数据,请执行以下操作:
In [11]: df.assign(**{"{}_product".format(cl): val*df.loc[:,cl]
...: for cl, val in weights.items()})
Out[11]:
id A B C A_product B_product C_product
0 -1.893885 0.940408 0.841350 -0.669378 0.094041 0.841350 -1.004067
1 -0.526427 0.472322 -0.546121 0.201615 0.047232 -0.546121 0.302423
2 -0.450193 -0.422066 0.564866 1.866878 -0.042207 0.564866 2.800318
这会产生一个新的数据帧,并且无法正常工作。我认为最简单的方法是从
dict
创建系列,它可以将索引与列名对齐:
In [13]: df.assign(**{cl: val*df.loc[:,cl]
...: for cl, val in weights.items()})
Out[13]:
id A B C
0 -1.893885 0.094041 0.841350 -1.004067
1 -0.526427 0.047232 -0.546121 0.302423
2 -0.450193 -0.042207 0.564866 2.800318
以及更通用的解决方案,谢谢:
这在dataframe和dictionary中都容纳了非重叠键
df[list(weights)] *= pd.Series(weights)
print (df)
id A B C
0 -0.641314 -0.052651 0.225116 -1.696711
1 0.018321 -0.094473 -0.123334 -1.280034
2 0.703119 0.046886 1.038572 -2.294585
注意:df
有id
而权重
没有<代码>权重
有D
而df
没有。此解决方案仅修改重叠的列。而且,它非常简洁
np.random.seed([3,1415])
df = pd.DataFrame(
np.random.randn(3,4),
columns='id A B C D'.split()
)
weights = dict(A=.1, B=1., C=1.5, D=2.)
df
id A B C
0 -2.129724 -1.268466 -1.970500 -2.259055
1 -0.349286 -0.026955 0.316236 0.348782
2 0.715364 0.770763 -0.608208 0.352390
这是否假定权重数组与df列的顺序相同?在我的实际数据中,它们不是,所以我需要一种方法来查找相应的weight@weights不是一个数组,它是一个dict
请查看我的数据,看看这是否是您想要的。是的。。。这是你的答案。保持它的通用性df[list(weights.keys())]*=pd.Series(weights)
非常好,但是您应该使用list(weights)
而不是list(weights.keys())
np.random.seed([3,1415])
df = pd.DataFrame(
np.random.randn(3,4),
columns='id A B C D'.split()
)
weights = dict(A=.1, B=1., C=1.5, D=2.)
df
id A B C
0 -2.129724 -1.268466 -1.970500 -2.259055
1 -0.349286 -0.026955 0.316236 0.348782
2 0.715364 0.770763 -0.608208 0.352390
df.update(df.mul(pd.Series(weights)).dropna(1))
df
id A B C
0 -2.129724 -0.126847 -1.970500 -3.388583
1 -0.349286 -0.002696 0.316236 0.523173
2 0.715364 0.077076 -0.608208 0.528586