I have the following dataset:
import pandas as pd from datetime import datetime import numpy as np date_rng = pd.date_range(start='2020-07-01', end='2020-07-10', freq='d') l1 = [np.nan, np.nan, "local_max", np.nan, np.nan, "local_min", np.nan, np.nan, "local_max", np.nan] l2 = [np.nan, np.nan, "local_max", np.nan, np.nan, "local_min", np.nan, np.nan, "local_max", "local_min"] df = pd.DataFrame({ 'date':date_rng, 'value':l1, 'group':'a' }) df2 = pd.DataFrame({ 'date':date_rng, 'value':l1, 'group':'b' }) df = df.append(df2, ignore_index=True) I want to calculate features,such as count of local_min and local_max per group and save it in a new dataframe with the desired output:
I able to calculate features but fail to apply it to the group in a elegant way:
columns = ["group", "local_min", "local_max"] df_features = pd.DataFrame([["a", 1, 2], ["b", 1, 3],], columns=columns) df_features Any help would be much appreciated!