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.

Marceloo
  • 13
  • 2

2 Answers2

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'
Pygirl
  • 12,969
  • 5
  • 30
  • 43
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]]))
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41