Reputation: 251
I have a dataframe with a column of values in the millions AND thousands:
pd.DataFrame({"Market Value":[25500000, 500000, 1200000, 250000]})
I need to convert the numbers into currency (€) in millions AND thousands. i.e. €25.5M, €500k, €1.2M, €250k
This previous post I have gotten the code to do this conversion but only for columns with values in the millions, not for columns with values in the millions and thousands:
df['Market Value (€)'] = '€' + (df['Market value'].astype(float)/1000000).round(2).astype(str) + 'M'
Is there a way to adjust this code to also convert values in the hundreds of thousands (i.e. 250000 to €250k)?
Upvotes: 0
Views: 358
Reputation: 126
This should give you the desired output.
df['Market Value (€)']=np.select(condlist=[(df['Market Value']/1000)>1000],choicelist=['€'+(df['Market Value'].astype(float)/1000000).round(2).astype(str) + 'M'],default='€'+(df['Market Value'].astype(float)/1000).round(2).astype(str) + 'K')
Output:
Upvotes: 2