OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python - How to change format of df column numbers to local currency

  • Thread starter Thread starter Peter Nguyen
  • Start date Start date
P

Peter Nguyen

Guest
I want to format the numbers in a df column into local currency.

i.e. original df:

Column AColumn B
50005%
200015%

If I was in the UK then the results of the table should be:

Column AColumn B
£ 5000.005%
£ 2000.0015%

and if I were using the table in the US it would convert to this instead:

Column AColumn B
$ 5000.005%
$ 2000.0015%

I have tried:

Code:
locale.currency(x, grouping=True)

but I cannot get it to apply to a df column.

Is there also a way to keep it a float to run calculation? And also format correctly if a calculation runs a negative it should display:

  • CCY XXX.YY

instead of

CCY - XXXX.YY

Thanks
<p>I want to format the numbers in a df column into local currency.</p>
<p>i.e. original df:</p>
<div class="s-table-container"><table class="s-table">
<thead>
<tr>
<th>Column A</th>
<th>Column B</th>
</tr>
</thead>
<tbody>
<tr>
<td>5000</td>
<td>5%</td>
</tr>
<tr>
<td>2000</td>
<td>15%</td>
</tr>
</tbody>
</table></div>
<p>If I was in the UK then the results of the table should be:</p>
<div class="s-table-container"><table class="s-table">
<thead>
<tr>
<th>Column A</th>
<th>Column B</th>
</tr>
</thead>
<tbody>
<tr>
<td>£ 5000.00</td>
<td>5%</td>
</tr>
<tr>
<td>£ 2000.00</td>
<td>15%</td>
</tr>
</tbody>
</table></div>
<p>and if I were using the table in the US it would convert to this instead:</p>
<div class="s-table-container"><table class="s-table">
<thead>
<tr>
<th>Column A</th>
<th>Column B</th>
</tr>
</thead>
<tbody>
<tr>
<td>$ 5000.00</td>
<td>5%</td>
</tr>
<tr>
<td>$ 2000.00</td>
<td>15%</td>
</tr>
</tbody>
</table></div>
<p>I have tried:</p>
<pre><code>locale.currency(x, grouping=True)
</code></pre>
<p>but I cannot get it to apply to a df column.</p>
<p>Is there also a way to keep it a float to run calculation? And also format correctly if a calculation runs a negative it should display:</p>
<ul>
<li>CCY XXX.YY</li>
</ul>
<p>instead of</p>
<p>CCY - XXXX.YY</p>
<p>Thanks</p>
 

Latest posts

Top