I am using Dask to read and save csvs.
import dask.dataframe as dd
I have a dataframe with 6 columns
date col1 col2 t1 t2 t3 col3
12 23.2 23.2 100 200 300 1
13 34.2 12.2 10 223 122 2
14 32 12 10 20 100 3
I want to conver this dataframe to:
date col1 col2 t_day t_val col3
12 23.2 23.2 1 100 1
12 23.2 23.2 2 200 1
12 23.2 23.2 3 300 1
13 34.2 12.2 1 10 2
13 34.2 12.2 2 223 2
13 34.2 12.2 3 122 2
14 32 12 1 10 3
14 32 12 1 20 3
14 32 12 1 100 3
Need to pivot on all columns that match 't*' so that all columns t1, t2, t3, .. can be pivoted. Can you please help?
I have tried using:
pivoted_df = (data.melt(id_vars=data.columns.difference(target_features),var_name='t_day', value_name='t_val')
.assign(t_day=lambda d: d['t_day'].astype('str').str.extractall('(\d+)').unstack().fillna('').sum(axis=1).astype(int))
)
But because dask doesnt have unstack operation:
AttributeError: 'DataFrame' object has no attribute 'unstack'