[英]Remove scientific notation from pandas column containing both string and float
[英]how to remove scientific notation in pandas from column object type
我有下面的數據保存在csv文件中的df:
A B
1 ABC 0.00772998456623635
2 XYZ -6.745157813050465e-05
3 PQR UDS
5 NA
6 TES
7 SEZ 0.051751390215281516
B列是對象類型,一些數字以科學計數法格式顯示。 我想壓制科學計數法。 我嘗試了以下方法,但沒有任何效果:(
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)
誰能幫我如何將數字格式從-6.745157813050465e-05更改為-0.0000674515781305046
有趣的問題。 熊貓astype
此問題,如果您使用errors='ignore'
,它將不會更改它。 它與一個簡單的自定義函數一起工作:
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)
然后,將其作為浮點數,可以使用pd.set_option('display.float_format', '{:.60g}'.format)
將其轉換為十進制數:
df
A B
1 ABC 0.007729984566236349982637499067550379550084471...
2 XYZ -6.74515781305046527565466574216657136275898665...
3 PQR UDS
5 NaN None
6 TES None
7 SEZ 0.051751390215281516116174742592193069867789745...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.