
比如我想把log_date里的年和月提取出來做新的一列,應該怎么操作呢?直接a.log_date[0:7]他會以為我想提取log_date這一列的第一到第六行。。。
uj5u.com熱心網友回復:
import pandas as pd
from pandas import DataFrame
from datetime import datetime
df=DataFrame({'log_date':['2013-08-01','2012-03-05','2012-03-01']})
print(df)
df['year']=df['log_date'].map(lambda x:datetime.strptime(x,'%Y-%m-%d').year)
df['month']=df['log_date'].map(lambda x:datetime.strptime(x,'%Y-%m-%d').month)
print(df)
log_date
0 2013-08-01
1 2012-03-05
2 2012-03-01
log_date year month
0 2013-08-01 2013 8
1 2012-03-05 2012 3
2 2012-03-01 2012 3
Process finished with exit code 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/186509.html
