What is the best approach to convert the column "price" to be in exponential form e.g laptop-->1.0E4?
import pandas as pd
data = {'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],
'price': [10000, 100000, 1000000, 45000, 2000]
}
df = pd.DataFrame(data)
df
Output:
product_name price
0 laptop 10000
1 printer 100000
2 tablet 1000000
3 desk 45000
4 chair 2000
CodePudding user response:
It's just a matter of formatting. You can set the default float format up front, and cast your int to float and print.
import pandas as pd
pd.set_option('display.float_format', '{:.1E}'.format)
data = {'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],
'price': [10000, 100000, 1000000, 45000, 2000]
}
df = pd.DataFrame(data)
df['price'] = df['price'].astype(float)
print(df)
Output
product_name price
0 laptop 1.0E 04
1 printer 1.0E 05
2 tablet 1.0E 06
3 desk 4.5E 04
4 chair 2.0E 03
CodePudding user response:
I found this on Geeks for Geeks and made it as a function like this
def int2exp(n: int):
return "{:e}".format(n)