Spring
Spring

Reputation: 193

Numpy broadcasting and for loop

I have two pd.dataframes.

       df1

           Equipment_Class    0     1       2         3
               PC1         8.72     7.32    0.17    0.00
               PC2        19.18     10.11   8.72    0.35


       df2
            Year    Equipment_Class     0         1          2        3
            2024    PC1                0.7       0.3      0.1         0.0
            2025    PC1                0.6       0.3      0.1         0.0
            2026    PC1                0.6       0.3      0.1         0.0
            2027    PC2                0.4       0.5      0.1         0.0         
            2028    PC2                0.2       0.5      0.1         0.2
            2029    PC2                0.3       0.5      0.1         0.1

I want to multiply df1.loc[0, "0":"3"] and df2.loc[0:, "0":"3"] if Equipment_Class are match in both dfs, i.e., Equipment_class == "PC1" or "PC2"

I also want to do a for loop, so I don't have to manually add on columns.

Below is my code so far:

  df3=pd.DataFrame(columns=['PC1','PC2'])
  col_names=df1.Equipment_Class

  for incol,df3column in zip(col_names, df3.columns):
      df3[df3column]=df1.loc[df1.Equipment_Class==incol, "0":"3"].to_numpy() 
                   [None,:]*df2.loc[df2.Equipment_Class==incol, "0":"3"]

I got following error message: ValueError: Unable to coerce to Series/DataFrame, dim must be <= 2: (1, 1, 4)

Thank you for your help.

Upvotes: 1

Views: 44

Answers (1)

SKPS
SKPS

Reputation: 5835

df1=pd.read_csv('input.txt', delim_whitespace=True).set_index('Equipment_Class')
df2=pd.read_csv('input2.txt', delim_whitespace=True).set_index('Equipment_Class')
print(df1.mul(df2.drop('Year', 1), fill_value=1))

produces

                     0      1      2      3
Equipment_Class                            
PC1              6.104  2.196  0.017  0.000
PC1              5.232  2.196  0.017  0.000
PC1              5.232  2.196  0.017  0.000
PC2              7.672  5.055  0.872  0.000
PC2              3.836  5.055  0.872  0.070
PC2              5.754  5.055  0.872  0.035

Upvotes: 1

Related Questions