Get weekday/day-of-week for Datetime column of DataFrame

Use the new dt.dayofweek property: In [2]: df[‘weekday’] = df[‘Timestamp’].dt.dayofweek df Out[2]: Timestamp Value weekday 0 2012-06-01 00:00:00 100 4 1 2012-06-01 00:15:00 150 4 2 2012-06-01 00:30:00 120 4 3 2012-06-01 01:00:00 220 4 4 2012-06-01 01:15:00 80 4 In the situation where the Timestamp is your index you need to reset the index … Read more

How to get number of groups in a groupby object in pandas?

Simple, Fast, and Pandaic: ngroups Newer versions of the groupby API (pandas >= 0.23) provide this (undocumented) attribute which stores the number of groups in a GroupBy object. # setup df = pd.DataFrame({‘A’: list(‘aabbcccd’)}) dfg = df.groupby(‘A’) # call `.ngroups` on the GroupBy object dfg.ngroups # 4 Note that this is different from GroupBy.groups which … Read more

Good alternative to Pandas .append() method, now that it is being deprecated?

Create a list with your dictionaries, if they are needed, and then create a new dataframe with df = pd.DataFrame.from_records(your_list). List’s “append” method are very efficient and won’t be ever deprecated. Dataframes on the other hand, frequently have to be recreated and all data copied over on appends, due to their design – that is … Read more