python pandas replacing strings in dataframe with numbers

前端 未结 9 1558
名媛妹妹
名媛妹妹 2020-12-08 04:22

Is there anyway to use the mapping function or something better to replace values in an entire dataframe?

I only know how to perform the mapping on series.

I

9条回答
  •  粉色の甜心
    2020-12-08 04:28

    You can build dictionary from column values itself and fill like below

    x=df['Item_Type'].value_counts()
    item_type_mapping={}
    item_list=x.index
    for i in range(0,len(item_list)):
        item_type_mapping[item_list[i]]=i
    
    df['Item_Type']=df['Item_Type'].map(lambda x:item_type_mapping[x]) 
    

提交回复
热议问题