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

后端 未结 4 728
滥情空心
滥情空心 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:27

    This should work even over mixed types within columns but is likely slow over large dataframes.

    def mul(x, y):
        try:
            return pd.to_numeric(x) * y
        except:
            return x
    
    df.applymap(lambda x: mul(x, 3))
    

提交回复
热议问题