pandas: How to transform all numeric columns of a data frame into logarithms

Supposed you have a dataframe named df

You can first make a list of possible numeric types, then just do a loop

numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
for c in [c for c in df.columns if df[c].dtype in numerics]:
    df[c] = np.log10(df[c])

Or, a one-liner solution with lambda operator and np.dtype.kind

numeric_df = df.apply(lambda x: np.log10(x) if np.issubdtype(x.dtype, np.number) else x)

If most columns are numeric it might make sense to just try it and skip the column if it does not work:

for column in df.columns:
    try:
        df[column] = np.log10(df[column])
    except (ValueError, AttributeError):
        pass

If you want to you could wrap it in a function, of course.

If all columns are numeric, you can even simply do

df_log10 = np.log10(df)

Tags:

Python

Pandas