Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- #%% make some data frame for testing
- phi = np.linspace(0, 2*np.pi, 1024)
- df = pd.DataFrame({
- 'cos': np.cos(phi),
- 'sin': np.sin(phi),
- 'cosxsin': np.cos(phi) * np.sin(phi)
- }, index=phi)
- #%% function which resamples a dataframe to a new number of rows
- def resample_fixed(df, n_new):
- n_old, m = df.values.shape
- mat_old = df.values
- mat_new = np.zeros((n_new, m))
- x_old = np.linspace(df.index.min(), df.index.max(), n_old)
- x_new = np.linspace(df.index.min(), df.index.max(), n_new)
- for j in range(m):
- y_old = mat_old[:, j]
- y_new = np.interp(x_new, x_old, y_old)
- mat_new[:, j] = y_new
- return pd.DataFrame(mat_new, index=x_new, columns=df.columns)
- #%% resample the data frame and show the values
- df2 = resample_fixed(df, 100)
- print(df2.shape)
- df2.plot()
Add Comment
Please, Sign In to add comment