Home > other >  Excuse me everybody how to slice the elements in the data frame do?
Excuse me everybody how to slice the elements in the data frame do?

Time:10-24


Such as I want to have the year and month in the log_date extracted a new column, should how to operate? A. directly og_date [0:7] he will think I want to extract log_date first to the sixth row, a column,

CodePudding user response:

 import pandas as pd 
The from pandas import DataFrame
The from datetime import datetime

Df=DataFrame ({' log_date: [' 2013-08-01 ', '2012-03-05', '2012-03-01']})
Print (df)
Df [' year ']=df [' log_date]. The map (lambda x: datetime strptime (x, Y - '% % m - % d'). The year)
Df [' month ']=df [' log_date]. The map (lambda x: datetime strptime (x, Y - '% % m - % d'). The month)
Print (df)


Log_date
0, 2013-08-01
1 the 2012-03-05
2 the 2012-03-01
Log_date year month
0 2013-08-01 2013 8
1 2012-03-05 2012 3
2 the 2012-03-01 2012 3

Process finished with exit code 0
  • Related