convert a column in a python pandas from STRING MONTH into INT

前端 未结 2 767
你的背包
你的背包 2021-01-05 15:56

In Python 2.7.11 & Pandas 0.18.1:

If we have the following csv file:

YEAR,MONTH,ID
2011,JAN,1
2011,FEB,1
2011,MAR,1

Is there an

2条回答
  •  一向
    一向 (楼主)
    2021-01-05 16:16

    Following Max's last point; create the same thing but rely on your local dataframe's way of encoding months:

    # create mapping
    d = dict((v,k) for k,v in zip(range(1, 13), df.Month.unique()))
    # create column
    df['month_index'] = df['Month'].map(d)
    

提交回复
热议问题