繁体   English   中英

pandas 查找日期属于哪个半年

[英]pandas find which half year a date belongs to

我有以下带有date列的 pandas 数据框。 如何添加指定日期属于哪个半年的列?

在此处输入图像描述

将日期转换为日期时间,然后使用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'

  1. 构建pandas.DataFrame (不在提供的代码中)

(代码从这里开始)*

  1. 'date'列转换为日期时间
  2. 使用pandas.Seriesdf['date']提取月份作为pandas.Series.dt.month
  3. integer 除以 6 得到学期为pandas.Series of int (从 0 开始计数)
  4. 将学期字符串作为pandas.Series使用pandas.Series.map将学期整数作为学期名称列表上的索引
  5. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM