简体   繁体   English

Pandas:将从 DataFrame 中提取的值乘以另一个 DataFrame 中的列值

[英]Pandas: Multiplying a value extracted from a DataFrame to column values in another DataFrame

What I have:我有的:

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的某些列相同。

What I want:我想要的是:

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.我想对所有列进行迭代。

What I have done so far:到目前为止我做了什么:

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)

Problem:问题:

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中的相应权Weightmultiply

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.

相关问题 根据从Pandas的json列中提取的值创建数据框 - Creating a dataframe from values extracted from a json column in Pandas 基于来自另一个 DataFrame 的列乘以行值 - Multiplying row values based on column from another DataFrame 从另一个数据帧中的列值替换pandas数据帧中的列中的值 - Replacing value in a column in pandas dataframe from a column value in another dataframe pandas 在列值匹配时使用来自另一个数据帧的值更新数据帧 - pandas update a dataframe with values from another dataframe on the match of column values 熊猫:从另一列修改数据框中的值 - pandas: modifying values in dataframe from another column 如果数据框中的另一列使用pandas匹配某个值,则从数据框中的列中减去值 - substract values from column in dataframe if another column in dataframe matches some value using pandas 熊猫:从另一个数据框中的列值插入数据框中的行 - pandas: insert rows in a dataframe from column values in another dataframe 使用来自另一个DataFrame的值将列有效地添加到Pandas DataFrame - Efficiently add column to Pandas DataFrame with values from another DataFrame Pandas:根据另一个数据框中的值更新数据框中的多列 - Pandas : Updating multiple column in a dataframe based on values from another dataframe Pandas数据框根据查询数据框中的值选择行,然后根据列值选择其他条件 - Pandas Dataframe Select rows based on values from a lookup dataframe and then another condition based on column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM