Pandas: How to apply numpy function to every column
You can use df.transform(func, axis=0)
to apply a numpy function. This leverages the fact that numpy functions work with pandas Series
objects.
Example based on How to create pandas time series DataFrame example dataset:
# Load pre-built time series example dataset
df = pd.read_csv("https://datasets.techoverflow.net/timeseries-example.csv", parse_dates=["Timestamp"])
df.set_index("Timestamp", inplace=True)
# np.square will be called individually for each column
new_df = df.transform(np.square, axis=0)
Output
Original time series:
Full example code
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
# Load pre-built time series example dataset
df = pd.read_csv("https://datasets.techoverflow.net/timeseries-example.csv", parse_dates=["Timestamp"])
df.set_index("Timestamp", inplace=True)
# np.sqrt will be called individually for each column
new_df = df.transform(np.square, axis=0)
# Plot subsection of original DF for better visibility
df.iloc[:len(df)//2].plot()
plt.gcf().set_size_inches(10,5)
plt.savefig("Normal-Timeseries.svg")
# Plot subsection of transformed DF for better visibility
new_df.iloc[:len(df)//2].plot()
plt.gcf().set_size_inches(10,5)
plt.savefig("Square-Timeseries.svg")