How to use math.log10 function on whole pandas dataframe
Use the numpy version, not math
import numpy as np
np.log10(df)
You may want to use the applymap
method to apply math.log10
on the whole dataframe, here is the documentation.
You can test it:
df.applymap(math.log10)
From what it seems math.log10
cannot handle neither pandas dataframes nor ndarrays.
So one option would be to go with numpy, which also includes a function to compute the base 10 logarithm, np.log10
, and reconstruct the dataframe as pointed out in other solutions.
Or if you want to go with math.log10
, and the same would apply to other functions that cannot be directly vectorized, you can use DataFrame.applymap
to apply math.log10
to the dataframe elementwise. Do note however that this solution will be slower than a vectorized approach using np.log10
.
Use case
Here's an example of how this could be done using DataFrame.applymap
:
df = pd.DataFrame(np.random.randint(1,5,(6,6)), columns=list('abcdef'))
print(df)
a b c d e f
0 3 4 1 1 2 1
1 4 4 4 3 4 1
2 4 3 3 1 4 1
3 3 4 1 3 1 1
4 1 2 3 4 2 1
5 1 3 3 1 4 3
df.applymap(math.log10)
a b c d e f
0 0.477121 0.602060 0.000000 0.000000 0.30103 0.000000
1 0.602060 0.602060 0.602060 0.477121 0.60206 0.000000
2 0.602060 0.477121 0.477121 0.000000 0.60206 0.000000
3 0.477121 0.602060 0.000000 0.477121 0.00000 0.000000
4 0.000000 0.301030 0.477121 0.602060 0.30103 0.000000
5 0.000000 0.477121 0.477121 0.000000 0.60206 0.477121
For the numpy
solution, you could take the np.log10
of the dataframe, and reconstruct it as:
pd.DataFrame(np.log10(data), index=df.index, columns=df.columns)