[英]How to extract year, month, date from a date column?
先做df.datecolumn = pd.to_datetime(df.datecolumn)
,然后過上幸福的生活。
這將為您提供該月的年、月和日。 您還可以輕松獲取一年中的一周和一周中的一天。
import pandas as pd
df = pd.DataFrame(data=[['1920-01-01'], ['2008-12-06']], columns=['Date'])
df['Date'] = pd.to_datetime(df['Date'])
df['Year'] = df['Date'].apply(lambda x : x.year)
df['Month'] = df['Date'].apply(lambda x : x.month)
df['Day'] = df['Date'].apply(lambda x : x.day)
print(df)
在您的Time
列表中,您有一個錯字Dayorweek
應該是dayofweek
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.