I have df with below data which I am saving in csv file :
A B
1 ABC 0.00772998456623635
2 XYZ -6.745157813050465e-05
3 PQR UDS
5 NA
6 TES
7 SEZ 0.051751390215281516
Column B is of object type and some number are showing as scientific notation format. I wanted to suppress scientific notation. I tried below methods but nothing worked :(
np.set_printoptions(suppress=True)
df.to_csv('test.csv',sep='|',index=False, header=False,float_format='%f')
pd.set_option('display.float_format', '{:.60g}'.format)
Can any one help me How I can change number format from -6.745157813050465e-05 to -0.0000674515781305046
Interesting question. Pandas astype
can't manage this, and if you use errors='ignore'
it won't change it. It works with a simple custom func:
def to_float_custom(x):
if not pd.isna(x):
try:
return float(x)
except ValueError:
pass
return x
df.B = df.B.apply(to_float_custom)
Then you have it as float, and can use pd.set_option('display.float_format', '{:.60g}'.format)
to get it as a decimal:
df
A B
1 ABC 0.007729984566236349982637499067550379550084471...
2 XYZ -6.74515781305046527565466574216657136275898665...
3 PQR UDS
5 NaN None
6 TES None
7 SEZ 0.051751390215281516116174742592193069867789745...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.