pandas, multiply all the numeric values in the data frame by a constant

后端 未结 4 730
滥情空心
滥情空心 2021-01-01 17:37

How to multiply all the numeric values in the data frame by a constant without having to specify column names explicitly? Example:

In [13]: df = pd.DataFrame         


        
4条回答
  •  难免孤独
    2021-01-01 18:17

    The other answer specifies how to multiply only numeric columns. Here's how to update it:

    df = pd.DataFrame({'col1': ['A','B','C'], 'col2':[1,2,3], 'col3': [30, 10,20]})
    
    s = df.select_dtypes(include=[np.number])*3
    
    df[s.columns] = s
    
    print (df)
    
      col1  col2  col3
    0    A     3    90
    1    B     6    30
    2    C     9    60
    

提交回复
热议问题