python - Transform category, start_time, end_time DateFrame for plotting in pandas -
i have pandas dataframe:
df.info() <class 'pandas.core.frame.dataframe'> int64index: 32656 entries, 94418 2 data columns (total 8 columns): customer_id 32656 non-null object session_id 32656 non-null int64 start 32656 non-null datetime64[ns, america/los_angeles] end 32656 non-null datetime64[ns, america/los_angeles] length 32656 non-null timedelta64[ns] category 32656 non-null object rounded_start 32656 non-null datetime64[ns, america/los_angeles] rounded_end 32656 non-null datetime64[ns, america/los_angeles] dtypes: datetime64[ns, america/los_angeles](4), int64(1), object(2), timedelta64[ns](1) memory usage: 2.2+ mb
i create datetimeindex:
rng = pd.date_range(df['rounded_start'].min(), end=df['rounded_start'].max(), freq='5min')
how tie 2 datasets can plot each point in range on x-axis , shows count of how many categories included during time?
i suspect work though haven't verified.
df_count = pd.dataframe(index=rng) def count_cats(x, df): date = x.name[0] condition1 = df.start <= date condition2 = df.end >= date df_slice = df.loc[condition1 & condition2, 'category'] return pd.series([df_slice.unique().size], index=['countcats']) df_count = df_count.apply(lambda x: count_cats(x, df))