Converting string objects to int/float using pandas

后端 未结 2 629
时光取名叫无心
时光取名叫无心 2021-02-04 01:18
import pandas as pd

path1 = \"/home/supertramp/Desktop/100&life_180_data.csv\"

mydf =  pd.read_csv(path1)

numcigar = {\"Never\":0 ,\"1-5 Cigarettes/day\" :1,\"10-         


        
2条回答
  •  你的背包
    2021-02-04 01:54

    Try using this function for all problems of this kind:

    def get_series_ids(x):
        '''Function returns a pandas series consisting of ids, 
           corresponding to objects in input pandas series x
           Example: 
           get_series_ids(pd.Series(['a','a','b','b','c'])) 
           returns Series([0,0,1,1,2], dtype=int)'''
    
        values = np.unique(x)
        values2nums = dict(zip(values,range(len(values))))
        return x.replace(values2nums)
    

提交回复
热议问题