[英]Python: Pandas dataframe get the year to which the week number belongs and not the year of the date
[英]pandas find which half year a date belongs to
将日期转换为日期时间,然后使用numpy.where
与比较小于或等于:
df['date'] = pd.to_datetime(df['date'], dayfirst=True)
df['half year'] = np.where(df['date'].dt.month.le(6), 'H1', 'H2')
print (df)
date half year
0 1993-09-09 H2
1 1993-09-11 H2
2 1994-01-23 H1
3 1993-03-18 H1
没有numpy
的解决方案,将掩码更改为更大的6
,加1
并转换为字符串:
df['date'] = pd.to_datetime(df['date'], dayfirst=True)
df['half year'] = 'H' + df['date'].dt.month.gt(6).add(1).astype(str)
print (df)
date half year
0 1993-09-09 H2
1 1993-09-11 H2
2 1994-01-23 H1
3 1993-03-18 H1
尝试:
df['half year'] = 'H' + pd.to_datetime(df['date']).dt.month.floordiv(6).add(1).astype(str)
print(df)
# Output
date half year
0 09-09-1993 H2
1 18-03-1993 H1
此解决方案使用.apply
方法。
>>> import pandas as pd
>>>
>>> df = pd.DataFrame({'date': ['09-09-1993', '11-09-1993', '23-01-1994', '18-03-1993']})
>>>
>>> df['date'] = pd.to_datetime(df['date'], format='%d-%m-%Y')
>>> df
date
0 1993-09-09
1 1993-09-11
2 1994-01-23
3 1993-03-18
>>>
>>> df['half year'] = df.date.dt.month.apply(lambda x: "H1" if x in range(0, 7) else "H2")
>>> df
date half year
0 1993-09-09 H2
1 1993-09-11 H2
2 1994-01-23 H1
3 1993-03-18 H1
如果没有numpy
,这是一个相当通用的解决方案,可以轻松修改以将学期设为'first'
或'second'
:
pandas.DataFrame
(不在提供的代码中)(代码从这里开始)*
'date'
列转换为日期时间pandas.Series
从df['date']
提取月份作为pandas.Series.dt.month
pandas.Series
of int
(从 0 开始计数)pandas.Series
使用pandas.Series.map
将学期整数作为学期名称列表上的索引pandas.Categorical
捆绑(步骤 2. 至 4.)以获取分类列(空间更小,处理速度更快),此步骤是可选的>>> df['date'] = pd.to_datetime(df['date'])
>>> df['half year'] = pd.Categorical((df['date'].dt.month // 6).map(lambda h:['H1','H2'][h]))
>>> df
date half year
0 1993-09-09 H2
1 1993-09-11 H2
2 1994-01-23 H1
3 1993-03-18 H1
>>> df.dtypes
date datetime64[ns]
half year category
dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.