I have a question about how to handle the data frame. You want to find the product of two dfs with the same column name and different index names.

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

Column names are all the same You are trying to find the product of two files with different index names. And I want to use the index name of the multiplied value as the index name of df1.

df3 = df1.mul(df2, fill_value=1)

Since I'm writing it like this Two indexes were combined into a join expression.

The calculation was done through the command prod Shows output without index name.

I want to multiply the values of the two data frames and output the result of the multiplied values. I want the index name of the result value to be the index name in df1.

If anyone knows, please answer.

pandas dataframe

2022-09-20 10:47

1 Answers

>>> df1 = pd.DataFrame({"a":[1,2,3], "b":[4,5,10]}, index=[0,1,2])
>>> df1
   a   b
0  1   4
1  2   5
2  3  10
>>> df2 = pd.DataFrame({"a":[11,12,13], "b":[14,15,10]}, index=[0,3,4])
>>> df2
    a   b
0  11  14
3  12  15
4  13  10

If you simply try to multiply data frames, you calculate them by matching the index.

>>> df1*df2
      a     b
0  11.0  56.0
1   NaN   NaN
2   NaN   NaN
3   NaN   NaN
4   NaN   NaN
>>> df1.values
array([[ 1,  4],
       [ 2,  5],
       [ 3, 10]], dtype=int64)

The values of the data frame are a numpy array of data frame contents. Pure array with no indexes.

>>> df1.values * df2.values
array([[ 11,  56],
       [ 24,  75],
       [ 39, 100]], dtype=int64)
>>> df1 * df2.values
    a    b
0  11   56
1  24   75
2  39  100

>>> df3 = df1*df2.values
>>> df3
    a    b
0  11   56
1  24   75
2  39  100
>>> 

df1 can be multiplied by a numpy array of the same shape.


2022-09-20 10:47

If you have any answers or tips


© 2022 pinfo. All rights reserved.