import datetime detester = ‘2017-01-01' date = datetime.datetime.strptime(detester,’%Y-%m-%d')
1 import datetime 2 3 date = datetime.now() 4 5 detester = date.strftime(‘%Y-%m-%d')
# td['purchase-date'] 是數據框 dataframe中的一列 for x in td['purchase-date']: nPos = x.index('T') print(x[:nPos]) # 上面方法 等同於 [x[:x.index('T')]) for x in td['purchase-date']]
import numpy as np import pandas as pd import datetime #把字符串轉成datetime def string_toDatetime(string): return datetime.datetime.strptime(string, "%Y-%m-%d") data = pd.DataFrame([string_toDatetime(x[:x.index('T')]) for x in td['purchase-date']],columns=['purchase-date']) #截取字符串 # x.index("T") 獲取到T 在字符串中的位置, x 在 nArray td['purchase-date']中 x[:x.index('T')] 獲取 從0 到 字符串位置 的 子字符串
td.loc[:,'purchase-date'] = data['purchase-date'] # 把處理好的日期列 更新到 原來的 td 數據框內
按月統計 生成柱狀圖python
df1['month'] = df1['日期'].map(lambda x: x[ :x.rindex('-')])