python pandas replacing strings in dataframe with numbers

前端 未结 9 1566
名媛妹妹
名媛妹妹 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:34

    To convert Strings like 'volvo','bmw' into integers first convert it to a dataframe then pass it to pandas.get_dummies()

      df  = DataFrame.from_csv("myFile.csv")
      df_transform = pd.get_dummies( df )
      print( df_transform )
    

    Better alternative: passing a dictionary to map() of a pandas series (df.myCol) (by specifying the column brand for example)

    df.brand = df.brand.map( {'volvo':0 , 'bmw':1, 'audi':2} )
    

提交回复
热议问题