Reputation: 1251
Im using grouby to rearrange a dataframe that has a date time index and I'd like to be able to return the datetime index value at the first grouping. For example, if I group by the Number column, i'd like to return the index value at the first instance of each number. I'm able to use the Trade Type with the .iat[0] to get the Type at the first occurrence of number. How would i return the index value?
Number Type
01/02/2007 - 05:30 1 Down
01/02/2007 - 10:30 2 Up
01/02/2007 - 15:30 2 Up
01/02/2007 - 17:00 2 Up
01/02/2007 - 18:30 2 Down
01/02/2007 - 22:00 3 Down
01/03/2007 - 06:30 3 Down
01/03/2007 - 11:00 3 Down
01/04/2007 - 08:30 3 Down
01/04/2007 - 09:30 4 Down
01/04/2007 - 10:30 4 Down
01/05/2007 - 10:00 4 Down
01/07/2007 - 23:00 4 Down
01/08/2007 - 10:30 5 Down
this is the function i'm using to build a dictionary which will return a series that I can then apply to my dataframe.
def compact_view(groupby):
agg_dict = {'Type': groupby['Type'].iat[0],
'Time': groupby['Date_Time'].iat[0]<----?
}
return pd.Series(agg_dict, index=['Type','Time'])
So i'd want these returned:
01/02/2007 - 05:30 1
01/02/2007 - 10:30 2
01/02/2007 - 22:00 3
01/04/2007 - 09:30 4
01/08/2007 - 10:30 5
Hopefully this makes sense
Upvotes: 0
Views: 60