Pandas: select all dates with specific month and day

后端 未结 2 1288
天涯浪人
天涯浪人 2020-12-06 16:27

I have a dataframe full of dates and I would like to select all dates where the month==12 and the day==25 and add replace the zero in the xmas column with a 1.<

相关标签:
2条回答
  • 2020-12-06 17:08

    Pandas Series with datetime now behaves differently. See .dt accessor.

    This is how it should be done now:

    df.loc[(df['date'].dt.day==25) & (cust_df['date'].dt.month==12), 'xmas'] = 1
    
    0 讨论(0)
  • 2020-12-06 17:23

    Basically what you tried won't work as you need to use the & to compare arrays, additionally you need to use parentheses due to operator precedence. On top of this you should use loc to perform the indexing:

    df.loc[(df['date'].month==12) & (df['date'].day==25), 'xmas'] = 1
    
    0 讨论(0)
提交回复
热议问题