-1

I have a pandas dataframe, some rows are number, i want to convert them to thousand separator. Tried this thousands=','but not working, tried other solutions but how can i convert whole dataframe into thousand separator before to_csv

Example what i want: convert 31752000 to 31,752,000

Here is the code i'm trying:

url = 'https://finance.yahoo.com/quote/RDS-A/balance-sheet?p=RDS-A'
table = pandas.read_html(url, attrs={
    'class': 'Lh(1.7) W(100%) M(0)'}, header=0)
df = pandas.DataFrame(table[0])
print(df)

#Then save to csv
#df.to_csv('ydata.csv', index=False, header=True)
Sohan Das
  • 1,560
  • 2
  • 15
  • 16

1 Answers1

1

You could manually add the commas like this (only works with numbers smaller than 1,000,000,000):

thousandSeperatedStrings=[]
for x in df:
    string=""
    if(x>=1000000):
        x=x%1000000000
        string=str(int(x/1000000))+","
        x=x%1000000
        string=string+str(int(x/100000))
        x=x%100000
        string=string+str(int(x/10000))
        x=x%10000
        string=string+str(int(x/1000))+","
        x=x%1000
        string=string+str(int(x/100))
        x=x%100
        string=string+str(int(x/10))
        x=x%10
        string=string+str(int(x))
    elif(x>=1000):
        x=x%1000000
        string=string+str(int(x/1000))+","
        x=x%1000
        string=string+str(int(x/100))
        x=x%100
        string=string+str(int(x/10))
        x=x%10
        string=string+str(int(x))
    else:
        string=str(int(x))
    thousandSeperatedStrings.append(string)
Menroka
  • 19
  • 5