0

I have a pandas data frame with a column that represents dates as:

Name: ts_placed, Length: 13631, dtype: datetime64[ns]

It looks like this ( it was recoded using week as time unit):

0     42
1     44
2     44
3     41
4     43
5     45
6     46
...

What I want to do is to create a new pandas data frame for each week. In other words, I should obtain a data frame that contains only elements that have the value 42 in the column 'ts_placed' a data frame that contains only elements that have the value 43 in the column 'ts_placed' and so on.

jcoppens
  • 5,306
  • 6
  • 27
  • 47
Blue Moon
  • 4,421
  • 20
  • 52
  • 91
  • 2
    Why do you need this? Why not just `groupby` on `ts_placed` and access the individual groups? – EdChum Jun 26 '15 at 14:31
  • EdChum I need to divide them further and compute different operation on them. it would be more practical to treat them as different data frames – Blue Moon Jun 26 '15 at 14:36
  • 1
    But surely managing lots of dfs is more painful than just accessing the individual weeks as groups and performing operations on them? – EdChum Jun 26 '15 at 14:37
  • they are not too many and despite this i wanted to learn how to do it in general. Let's say you have only true and false in a column and you want to divide the dataframe in 2 according to this – Blue Moon Jun 26 '15 at 14:41
  • I agree with EdChum totally, as long as they share the column/index I really don't see the real benefits separating them into individual dataframes. In latter case True/False, a simple masking will just do fine – Anzel Jun 26 '15 at 14:42
  • 1
    Basically the general form would be `df_week_x = df[df['ts_placed'] == x]` where x would be your week number, the boolean condition will return a boolean series that masks the df – EdChum Jun 26 '15 at 14:46
  • that's what i needed. It worked just fine. Thank you very much. – Blue Moon Jun 26 '15 at 14:51

0 Answers0