[英]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.