我需要在 pandas 中创建一个 bool 列,这样奇数天会给我 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']中第一行的 bool 值
如何使索引 0 沿行向下递增?
最佳答案
尝试通过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
关于python - 如何根据 Pandas 中的奇数/偶数日期创建 bool 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68564735/