简体   繁体   中英

How to convert all the values in a column, from thousands to billions? Using Pandas

I wish to convert the columns imfGDP, gdpPerCapita and pop to billions.

[Data frame] https://i.stack.imgur.com/6JHHZ.png

Converting gdpPerCapita and pop to billions is really very easy. Just do this:

df['gdpPerCapita in bn)']=df['gdpPerCapita']/1000000 #converting thousands into billions
                                                     #by simply dividing each value with 
                                                     #1000000 and 
                                                     #saving it into new column.

df['pop in bn']=df['pop']/1000000      #Repeating same step for pop also

#New columns will automatically be added to dataframe

To convert imfGDP to billions, do this:

new_list=[]
for i in df['imfGDP']:
    j=i.replace('.','')
    k=j.split('e+')
    new_list.append(k)

main_list=[]
for value in new_list:
    value1=int(value[0])
    value2=int(value[1])
    value3=(value1*(10**value2))
    main_list.append(value3/1000000000)

df['imfGDP in bn']=main_list
#This new column will automatically be added to dataframe.

There you have it. All three columns converted to billions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM