Multiply two pandas DataFrames based on column

后端 未结 1 1545
广开言路
广开言路 2021-01-27 07:16

If I have two DataFrames how do I multiply them together by column to produce a DataFrame with the result. For example...

 df1 = pd.DataFrame(np.random.randint(1         


        
相关标签:
1条回答
  • 2021-01-27 07:58

    [CW; just to take this off the unanswered questions list..]

    I think you want df1 * df2. For example:

    >>> df1 = pd.DataFrame(np.random.randint(10, size=(5, 4)), columns=['A', 'B', 'C', 'D'])
    >>> df2 = pd.DataFrame(np.random.randint(10, size=(5, 4)), columns=['A', 'B', 'C', 'D'])
    >>> df1
       A  B  C  D
    0  0  6  5  7
    1  8  6  4  0
    2  6  2  4  3
    3  8  8  7  6
    4  8  7  9  0
    
    [5 rows x 4 columns]
    >>> df2
       A  B  C  D
    0  0  0  3  0
    1  8  5  6  5
    2  7  4  9  7
    3  8  4  4  1
    4  2  5  6  4
    
    [5 rows x 4 columns]
    >>> df1 * df2
        A   B   C   D
    0   0   0  15   0
    1  64  30  24   0
    2  42   8  36  21
    3  64  32  28   6
    4  16  35  54   0
    
    [5 rows x 4 columns]
    
    0 讨论(0)
提交回复
热议问题