2
$\begingroup$

I want to add a date column (from 1/1/1979 upto the data is) in pandas data frame.

Currently, my data frame looks like this:

0   1   2               3           4
0   1   654 31.457899   76.930397   0.2
1   1   711 31.507900   76.880402   0.2
2   1   712 31.507900   76.930397   0.2
3   1   713 31.507900   76.980400   0.2
4   1   714 31.507900   77.030403   0.2
5   1   715 31.507900   77.080399   0.2
6   1   716 31.507900   77.130402   0.2
7   1   717 31.507900   77.180397   0.2
8   1   718 31.507900   77.230400   0.2

Now I want to add one more column of data starting from 1/1/1979. How do I do that?

$\endgroup$
4
$\begingroup$

You can use the in-built date_range function from pandas library to generate dates and then add them to your dataframe. You can use it in the following way:

In [9]: import pandas as pd

In [10]: df = pd.DataFrame({'column1':[34,54,32,23,26]})

In [11]: df
Out[11]: 
   column1
0       34
1       54
2       32
3       23
4       26

In [12]: df['date'] = pd.date_range(start='1/1/1979', periods=len(df), freq='D')

In [13]: df
Out[13]: 
   column1       date
0       34 1979-01-01
1       54 1979-01-02
2       32 1979-01-03
3       23 1979-01-04
4       26 1979-01-05

Here I have incremented the dates day-wise per row. If you want to generate dates in some other way, check out the official documentation here.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.