Sam
Sam

Reputation: 61

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.sstatic.net/6JHHZ.png

Upvotes: 1

Views: 759

Answers (1)

ajay sagar
ajay sagar

Reputation: 199

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.

Upvotes: 1

Related Questions