1

I have a Dataframe like this :

  Date         sdate  
0 2012-3-12   [2012, 03, 12]
1 2012-3-25   [2012, 03, 25]
2 2012-4-20   [2012, 04, 20]
3 2012-4-12   [2012, 04, 12]
4 2012-4-26   [2012, 04, 26]

I need to extract the year,month and day to separate columns like this

           Date            sdate     year   month  day 
    0 2012-3-12   [2012, 03, 12]    2012      03   12
    1 2012-3-25   [2012, 03, 25]    2012      03   25 
    2 2012-4-20   [2013, 04, 20]    2013      04   20
    3 2012-4-12   [2015, 06, 12]    2015      06   12
    4 2012-4-26   [2011, 08, 26]    2011      08   26

Can I achieve this using for loop?

1 Answer 1

2

Use apply with pd.Series and rename the columns

In [784]: df.sdate.apply(pd.Series).rename(columns={0:'year',1:'month',2:'day'})
Out[784]:
   year  month  day
0  2012      3   12
1  2012      3   25
2  2012      4   20
3  2012      4   12
4  2012      4   26

join to original df

In [785]: df.join(df.sdate.apply(pd.Series).rename(columns={0:'year',1:'month',2:'day'}))
Out[785]:
        Date          sdate  year  month  day
0  2012-3-12  [2012, 3, 12]  2012      3   12
1  2012-3-25  [2012, 3, 25]  2012      3   25
2  2012-4-20  [2012, 4, 20]  2012      4   20
3  2012-4-12  [2012, 4, 12]  2012      4   12
4  2012-4-26  [2012, 4, 26]  2012      4   26

Or, provide column names as index

In [786]: df.sdate.apply(lambda x: pd.Series(x,  index=['year', 'month', 'day']))
Out[786]:
   year  month  day
0  2012      3   12
1  2012      3   25
2  2012      4   20
3  2012      4   12
4  2012      4   26
Sign up to request clarification or add additional context in comments.

4 Comments

i tried the first two lines of codes but after this when i do df.head() i am not getting the new dataframe. It still shows the old one
You need to assign the result back like df = df.join(df.sdate.apply.... to have the result stored in the new df.
I did it once and I got an error saying columns overlapping. But now it is working fine,I am using jupyter notebook
df.sdate.apply(lambda x: pd.Series(x, index=['year', 'month', 'day'])) what does this line do. can u please explain?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.