我正在尝试创建一个新列并将其添加为dataframe的Week。另外,我正在根据其他条件进行此操作。我在下面使用代码。
# List of the conditions
conditions = [
(df_sup[(df_sup['Date']>='2016-11-14') & (df_sup['Date']<'2016-11-21')]),
(df_sup[(df_sup['Date']>='2016-11-21') & (df_sup['Date']<'2016-11-28')]),
(df_sup[(df_sup['Date']>='2016-11-28') & (df_sup['Date']<'2016-12-05')]),
(df_sup[(df_sup['Date']>='2016-12-05') & (df_sup['Date']<'2016-12-12')]),
(df_sup[(df_sup['Date']>='2016-12-12') & (df_sup['Date']<'2016-12-19')])
]
# list of the values I want to assign for each condition
values = ['week1','week2','week3','week4','week5']
# Let's create a new column and use np.select to assign values to it using our lists as arguments
df_sup['Week'] = np.select(conditions, values)
但是,我遇到了以下错误。我该如何解决?
此外,我在下面共享数据。
答案 0 :(得分:1)
您的数据conditions
中不是条件列表,因为已使用boolean indexing
,所以请获取数据帧列表。
因此,对于条件列表,请删除df_sup[
,最后删除]
:
conditions = [
(df_sup['Date']>='2016-11-14') & (df_sup['Date']<'2016-11-21'),
(df_sup['Date']>='2016-11-21') & (df_sup['Date']<'2016-11-28'),
(df_sup['Date']>='2016-11-28') & (df_sup['Date']<'2016-12-05'),
(df_sup['Date']>='2016-12-05') & (df_sup['Date']<'2016-12-12'),
(df_sup['Date']>='2016-12-12') & (df_sup['Date']<'2016-12-19')
]