pythonpandasdataframenumericcalc

Pandas dataframe: convert dataframe numeric value to 2 to the power of this numeric value


enter image description here

How do I get the value of 2^(column of pandas dataframe) in another col of a dataframe.

For example:

Value 2^Value
0 1
1 2

Solution

  • You can use numpy.power :

    import numpy as np
    
    df["2^Value"] = np.power(2, df["Value"])
    

    Or simply, 2 ** df["Value"] as suggested by @B Remmelzwaal.

    Output :

    print(df)
    
       Value  2^Value
    0      0        1
    1      1        2
    2      3        8
    3      4       16
    

    Here is some stats/timing :

    enter image description here