簡體   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