日期时间到季节
Datetime to seasons
我有一个示例数据:
datetime. column1
2021-04-01 01:00. 11
2021-04-05 02:00. 10
2021-04-12 03:00. 1
2021-04-11 04:00. 5
2021-04-07 05:00. 20
我想创建一个名为 season 的新列,如果日期时间值介于 5 月和 9 月之间,则输出制冷季节,如果日期时间值介于 10 月和 4 月之间,则输出供暖季节。示例输出应如下所示:
datetime. column1. seasons
2021-01-01 01:00. 11. Heating season
2021-05-01 02:00. 10. Cooling season
2021-12-01 03:00. 1. Heating season
2021-11-01 04:00. 5. Heating season
2021-07-01 05:00. 20. Cooling season
方法很多,
df['datetime'] = pd.to_datetime(df.datetime)
带np.select带特定条件月
conditions = [
df.datetime.dt.month.isin(np.arange(5,10)),
(df.datetime.dt.month.isin(np.arange(1,5))) | (df.datetime.dt.month.isin(np.arange(10,13))),
]
choices = ['Cooling season','Heating season']
df['seasons'] = np.select(conditions, choices)
df
带np.select带默认值
conditions = [
df.datetime.dt.month.isin(np.arange(5,10)),
]
choices = ['Cooling season']
df['seasons'] = np.select(conditions, choices, default='Heating season')
df
和np.where
df['seasons'] = np.where(df.datetime.dt.month.isin(np.arange(5,10)), 'Cooling season','Heating season')
df
输出
datetime column1 seasons
0 2021-01-01 01:00:00 11 Heating season
1 2021-05-01 02:00:00 10 Cooling season
2 2021-12-01 03:00:00 1 Heating season
3 2021-11-01 04:00:00 5 Heating season
4 2021-07-01 05:00:00 20 Cooling season
df["seasons"] = np.where((df["datetime"].dt.month >= 5)
& (df["datetime"].dt.month <= 9),
"Cooling season", "Heating season")
>>> df
datetime column1 seasons
0 2021-01-01 01:00:00 11 Heating season
1 2021-05-05 02:00:00 10 Cooling season
2 2021-12-12 03:00:00 1 Heating season
3 2021-11-11 04:00:00 5 Heating season
4 2021-07-07 05:00:00 20 Cooling season
我有一个示例数据:
datetime. column1
2021-04-01 01:00. 11
2021-04-05 02:00. 10
2021-04-12 03:00. 1
2021-04-11 04:00. 5
2021-04-07 05:00. 20
我想创建一个名为 season 的新列,如果日期时间值介于 5 月和 9 月之间,则输出制冷季节,如果日期时间值介于 10 月和 4 月之间,则输出供暖季节。示例输出应如下所示:
datetime. column1. seasons
2021-01-01 01:00. 11. Heating season
2021-05-01 02:00. 10. Cooling season
2021-12-01 03:00. 1. Heating season
2021-11-01 04:00. 5. Heating season
2021-07-01 05:00. 20. Cooling season
方法很多,
df['datetime'] = pd.to_datetime(df.datetime)
带np.select带特定条件月
conditions = [
df.datetime.dt.month.isin(np.arange(5,10)),
(df.datetime.dt.month.isin(np.arange(1,5))) | (df.datetime.dt.month.isin(np.arange(10,13))),
]
choices = ['Cooling season','Heating season']
df['seasons'] = np.select(conditions, choices)
df
带np.select带默认值
conditions = [
df.datetime.dt.month.isin(np.arange(5,10)),
]
choices = ['Cooling season']
df['seasons'] = np.select(conditions, choices, default='Heating season')
df
和np.where
df['seasons'] = np.where(df.datetime.dt.month.isin(np.arange(5,10)), 'Cooling season','Heating season')
df
输出
datetime column1 seasons
0 2021-01-01 01:00:00 11 Heating season
1 2021-05-01 02:00:00 10 Cooling season
2 2021-12-01 03:00:00 1 Heating season
3 2021-11-01 04:00:00 5 Heating season
4 2021-07-01 05:00:00 20 Cooling season
df["seasons"] = np.where((df["datetime"].dt.month >= 5)
& (df["datetime"].dt.month <= 9),
"Cooling season", "Heating season")
>>> df
datetime column1 seasons
0 2021-01-01 01:00:00 11 Heating season
1 2021-05-05 02:00:00 10 Cooling season
2 2021-12-12 03:00:00 1 Heating season
3 2021-11-11 04:00:00 5 Heating season
4 2021-07-07 05:00:00 20 Cooling season