如何根据 Pandas 中的奇数/偶数日期创建布尔列?

时间:2021-07-28 17:14:57

标签: python pandas dataframe

我需要在 Pandas 中创建一个布尔列,这样奇数天会给我 True,偶数天会给我 False。

日期格式为2019-MM-DD,如2019-06-04。

2019-06-04 是偶数日,所以我想从中得到 False 2019-06-03 是个奇怪的日子,所以我想从中得到 True

我在想一些事情

df['New Column'] = int(df['Date'].astype(str).iloc[0][8:]) % 2 == 1

但是上面的代码只会返回['Date']中第一行的布尔值

如何使索引 0 向下递增行?

1 个答案:

答案 0 :(得分:2)

通过 day 属性尝试:

df['Date']=pd.to_datetime(df['Date'])
#ensure that 'Date' column is of dtype datetime
df['New Column'] =df['Date'].dt.day%2!=0
#Finally check if the day is even or not
相关问题