0

I am doing some web scraping and storing the results in a Pandas Dataframe to subsequently output to csv. My questions is: how can I add a timestamp to the top of the Dataframe to record when I did the web scraping?

For example:

df1 = pd.DataFrame({'Fruits':['Apple','Banana','Cherry']})

would print:

  Fruits
0  Apple
1 Banana
2 Cherry

how can I print:

9/9/2017
  Fruits
0  Apple
1 Banana
2 Cherry
cs95
  • 379,657
  • 97
  • 704
  • 746
Eric Choi
  • 785
  • 2
  • 7
  • 14

1 Answers1

0

Option 1

Setting df.index.name

df1.index.name = pd.Timestamp('9/9/2017')
df1

                     Fruits
2017-09-09 00:00:00        
0                     Apple
1                    Banana
2                    Cherry

Option 2

Using df.rename_axis:

df1 = df1.rename_axis(pd.Timestamp('9/9/2017'))
df1

                     Fruits
2017-09-09 00:00:00        
0                     Apple
1                    Banana
2                    Cherry
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Yes, it works for me. However, when I convert df1 to csv using df1.to_csv, the timestamp doesn't print out. Any idea why? – Eric Choi Sep 09 '17 at 05:07
  • @EricChoi You could instead do `df1.index.name = pd.Timestamp('9/9/2017')`. That retains it. I'm not sure why the former doesn't work. – cs95 Sep 09 '17 at 05:14
  • @COLDSPEED Works very well now. Thank you very much. – Eric Choi Sep 09 '17 at 07:58