0

I have a a csv file called "amazon_responded.csv" in which I am currently trying to format the dates in one of the columns in the file.

enter image description here

I need to format this date column called "tweet_created_at" into the format of "Nov 01". Ultimately I need to group the data by day, but I cant figure out how to format the date column into the format of "Nov 01".

I have tried using pd.to_datetime but i just can crack the right format.

2 Answers 2

0

After you use pd.to_datetime() method just do this:-

df['tweet_created_at']=df['tweet_created_at'].apply(lambda x:x.ctime().split(' '))
df['tweet_created_at']=df['tweet_created_at'].apply(lambda x:' '.join([x[1],x[3]]))
Sign up to request clarification or add additional context in comments.

Comments

0

You want to convert it into Nov 01 format because you want to group them by this right?

You can do it without formatting also.

by:

df['tweet_created_at'] = pd.to_datetime(df['tweet_created_at'])

Groupby Day and month:

df.groupby([df['tweet_created_at'].day, df['tweet_created_at'].month])

If you want to convert them into Nov 01 form:

df['new_date'] = df['tweet_created_at'].strftime('%b') + ' ' + df['tweet_created_at'].strftime('%d')

sample code:

st = "Tue Nov 01 01:57:25 +0000 2016"

st = pd.to_datetime(st)
date = st.strftime('%b') + ' ' + st.strftime('%d')

date:

'Nov 01'

2 Comments

Any idea as to why i am getting an error: " 'Series' object has no attribute 'strftime' " ?
@Marceloo: try using dt stackoverflow.com/a/33967346/6660373

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.