Pandas: convert dtype 'object' to int

后端 未结 8 2009
南旧
南旧 2020-11-29 02:02

I\'ve read an SQL query into Pandas and the values are coming in as dtype \'object\', although they are strings, dates and integers. I am able to convert the date \'object\'

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 02:11

    It's simple

    pd.factorize(df.purchase)[0]
    

    Example:

    labels, uniques = pd.factorize(['b', 'b', 'a', 'c', 'b'])`
    
    labels
    # array([0, 0, 1, 2, 0])
    
    uniques
    # array(['b', 'a', 'c'], dtype=object)
    

提交回复
热议问题