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

后端 未结 3 710
感动是毒
感动是毒 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:47

    First extract units as last character in strings. Then convert values without units to floats and multiply where needed:

    df = pd.DataFrame({'Market Cap':['6.46M','2.25B','0.23B']})
    units = df['Market Cap'].str[-1]
    df['Market Cap'] = df['Market Cap'].str[:-1].astype(float)
    df.loc[units=='M','Market Cap'] *= 0.001
    #    Market Cap
    # 0     0.00646
    # 1     2.25000
    # 2     0.23000
    

    Now everything is in billions.

提交回复
热议问题