Subtract a Series from a DataFrame while keeping the DataFrame struct intact

后端 未结 3 1734
无人及你
无人及你 2020-12-10 15:05

How can I subtract a Series from a DataFrame, while keeping the DataFrame struct intact?

df = pd.DataFrame(np.zeros((5,3)))
s = pd.Series(np.ones(5))

df - s         


        
3条回答
  •  甜味超标
    2020-12-10 16:06

    Maybe:

    >>> df = pd.DataFrame(np.zeros((5,3)))
    >>> s = pd.Series(np.ones(5))
    >>> df.sub(s,axis=0)
       0  1  2
    0 -1 -1 -1
    1 -1 -1 -1
    2 -1 -1 -1
    3 -1 -1 -1
    4 -1 -1 -1
    
    [5 rows x 3 columns]
    

    or, for a more interesting example:

    >>> s = pd.Series(np.arange(5))
    >>> df.sub(s,axis=0)
       0  1  2
    0  0  0  0
    1 -1 -1 -1
    2 -2 -2 -2
    3 -3 -3 -3
    4 -4 -4 -4
    
    [5 rows x 3 columns]
    

提交回复
热议问题