Jessica
Jessica

Reputation: 265

how to add column as year-month using pandas

I want to create and add one column year-month as index. so how to do it ? my dataset is contain 168 raws.

   23.59
   26.931
   24.740
   25.806
   24.364
   24.477
   23.901
     .
     .

i want dataset which is look like this:

 Year-Month   product
  1990-1       23.59
  1990-2       26.931  
  1990-3       24.740  
   ...          ...
  1991-1
   ...

how to do this please help..

Upvotes: 1

Views: 47

Answers (1)

Zero
Zero

Reputation: 76917

Use

In [263]: df['Y_M'] = pd.date_range(
                           start='1990-01-01', 
                           periods=len(df.index), freq='MS').strftime('%Y-%m')

In [264]: df
Out[264]:
   product      Y_M
0   23.590  1990-01
1   26.931  1990-02
2   24.740  1990-03
3   25.806  1990-04
4   24.364  1990-05
5   24.477  1990-06
6   23.901  1990-07

Upvotes: 4

Related Questions