[英]Pandas: Multiplying a value extracted from a DataFrame to column values in another DataFrame
I have two DataFrames.我有两个数据框。 The first one df1
contains cartesian coordinates and some values related to some elements.第一个df1
包含笛卡尔坐标和与某些元素相关的一些值。 It looks like this:它看起来像这样:
X Y Zn Pb Ag Cu Mo Cr Ni Co Ba
0 431434.79 3305001.94 87 7 0.02 42 2 57 38 14 393
1 432522.60 3298058.43 70 6 0.02 56 2 27 29 20 404
2 438045.35 3291987.05 75 5 0.02 69 2 44 23 17 417
3 436260.76 3294412.90 70 6 0.02 54 1 20 19 12 377
4 439294.48 3297653.81 168 14 0.02 27 2 31 18 14 277
5 441406.54 3298650.88 87 11 0.02 37 2 25 28 14 797
6 431189.97 3315353.72 113 11 0.02 40 1 110 36 13 361
7 428527.24 3315392.79 133 9 0.02 45 2 73 42 16 414
8 430881.58 3316951.45 144 8 0.02 58 1 83 39 21 617
9 427968.68 3317058.60 267 11 0.02 37 1 285 43 17 292
Now, I have another DataFrame, df2
, that contains some calculations.现在,我有另一个 DataFrame, df2
,其中包含一些计算。 It looks like this:它看起来像这样:
Prediction Rate (%) Occupied Area (%) Normalized Density Weight
Zn 50 50 1.00 0.000000
Pb 50 50 1.00 0.000000
Ag 78 22 3.55 1.266948
Cu 90 10 9.00 2.197225
Mo 79 21 3.76 1.324419
Cr 69 31 2.23 0.802002
Ni 81 19 4.26 1.449269
Co 70 30 2.33 0.845868
Ba 79 21 3.76 1.324419
As evident, the index of df2
is the same as some columns of df1
.很明显, df2
的索引与df1
的某些列相同。
Now I want to multiply Weight
of each element from df2
to the corresponding column of each element from df1
.现在我想将df2
中每个元素的权Weight
乘以df1
中每个元素的相应列。 For example, the weight of Zn
is 0
.例如, Zn
的权重为0
。 I want to multiply 0
to all of the values of Zn
in df1
.我想将0
乘以df1
中的所有Zn
值。 It would be basically a column of zeros.它基本上是一列零。 And I want to iterate that for all the columns.我想对所有列进行迭代。
I have extracted the Weights
column from df2
.我已经从df2
中提取了Weights
列。 Then I tried to create a new DataFrame by multiplication.然后我尝试通过乘法创建一个新的 DataFrame。
# list of each element
elements = ['Zn', 'Pb', 'Ag', 'Cu', 'Mo', 'Cr', 'Ni', 'Co', 'Ba']
# extracting "Weight" column from df2
weights_extracted = df1["Weight"]
# creating a new dataframe and multiplying values of each element with their corresponding weight
new_df = pd.DataFrame()
for i,element in enumerate(elements):
df2[element] = df2[element] * weights_extracted.loc[element,:]
new_df = new_df.append(raw_data,False)
I get this error:我收到此错误:
Too many indexers
How can I solve this issue?我该如何解决这个问题?
You can use the index of df2
to select the required columns in df1
, then multiply
those columns with the corresponding Weight
values from df2
您可以使用df2
的索引在df1
中选择所需的列,然后将这些列与df2
中的相应权Weight
值multiply
df1.update(df1[df2.index].mul(df2['Weight']))
print(df1)
X Y Zn Pb Ag Cu Mo Cr Ni Co Ba
0 431434.79 3305001.94 0.0 0.0 0.025339 92.283450 2.648838 45.714114 55.072222 11.842152 520.496667
1 432522.60 3298058.43 0.0 0.0 0.025339 123.044600 2.648838 21.654054 42.028801 16.917360 535.065276
2 438045.35 3291987.05 0.0 0.0 0.025339 151.608525 2.648838 35.288088 33.333187 14.379756 552.282723
3 436260.76 3294412.90 0.0 0.0 0.025339 118.650150 1.324419 16.040040 27.536111 10.150416 499.305963
4 439294.48 3297653.81 0.0 0.0 0.025339 59.325075 2.648838 24.862062 26.086842 11.842152 366.864063
5 441406.54 3298650.88 0.0 0.0 0.025339 81.297325 2.648838 20.050050 40.579532 11.842152 1055.561943
6 431189.97 3315353.72 0.0 0.0 0.025339 87.889000 1.324419 88.220220 52.173684 10.996284 478.115259
7 428527.24 3315392.79 0.0 0.0 0.025339 98.875125 2.648838 58.546146 60.869298 13.533888 548.309466
8 430881.58 3316951.45 0.0 0.0 0.025339 127.439050 1.324419 66.566166 56.521491 17.763228 817.166523
9 427968.68 3317058.60 0.0 0.0 0.025339 81.297325 1.324419 228.570570 62.318567 14.379756 386.730348
In the for loop 'i' var is not necessary, the var that you are using is 'element'.在 for 循环中 'i' var 不是必需的,您使用的 var 是 'element'。
Saying “thanks” is appreciated, but it doesn't answer the question.说“谢谢”是值得赞赏的,但这并不能回答问题。 Instead, vote up the answers that helped you the most!相反,请投票选出对您最有帮助的答案! If these answers were helpful to you, please consider saying thank you in a more constructive way – by contributing your own answers to questions your peers have asked here.如果这些答案对您有帮助,请考虑以更具建设性的方式表示感谢——通过对您的同行在这里提出的问题贡献您自己的答案。
I would solve this using numpy instead of pandas just for the readability.为了可读性,我会使用 numpy 而不是 pandas 来解决这个问题。 So I would recommend this所以我会推荐这个
elements = ['Zn', 'Pb', 'Ag', 'Cu', 'Mo', 'Cr', 'Ni', 'Co', 'Ba']
df1_matrix = df1[elements].to_numpy()
df2_matrix = df2['Weight'].to_numpy()
multiplied = df1_matrix * df2_matrix
new_df = pd.DataFrame(multiplied,columns=elements)
This makes the problem into a relatively simple matrix problem instead of dealing with indexes and for loops.这使得问题变成了一个相对简单的矩阵问题,而不是处理索引和 for 循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.