How to convert numbers represented as characters for short into numeric in Python

后端 未结 3 706
感动是毒
感动是毒 2020-12-20 16:54

I have a column in my data frame which has values like \'3.456B\' which actually stands for 3.456 Billion (and similar notation for Million). How to convert this string form

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-20 17:41

    I'd use a dictionary to replace the strings then evaluate as float.

    mapping = dict(K='E3', M='E6', B='E9')
    
    df['Market Cap'] = pd.to_numeric(df['Market Cap'].replace(mapping, regex=True))
    

提交回复
热议问题