Is there a way to suppress scientific notation in Panda's outputs without forcing a particular precision across all columns?
So that a data frame:
import pandas as pd
df = pd.DataFrame({"a": [0.01, 0.02, 0.03], "b": [0.0000001, 0.0000002, 0.0000003]})
df.to_csv(
"df.csv",
index=False,
)
That initially would be outputted as:
a | b |
---|---|
0.01 | 1.00E-07 |
0.02 | 2.00E-07 |
0.03 | 3.00E-07 |
Instead becomes my desired output:
a | b |
---|---|
0.01 | 0.0000001 |
0.02 | 0.0000002 |
0.03 | 0.0000003 |
Many questions about suppressing scientific notation in Pandas' .to_csv results have already been asked, but all of the answers involve specifying an arbitrary precision.
For instance, setting float_format="%.7f"
in df.to_csv
forces 7 significant digits for all float columns and numbers (and so does round(7)
, of course).
This would lead to the following output, which I don't want:
a | b |
---|---|
0.0100000 | 0.0000001 |
0.0200000 | 0.0000002 |
0.0300000 | 0.0000003 |
(I also tried using np.format_float_positional
as suggested here, but had no luck.)
CodePudding user response:
Implement np.format_float_positiona
l on a series. If done on df, you will be forced to iterate which can be quite computationally expensive.
Pd.Series
df['b'] =[(lambda x: np.format_float_positional(x))(x) for x in df['b']]
or simply as suggested by @user2357112 supports Monica
df['b'] =[np.format_float_positional(x) for x in df['b']]
Def function Lets try putting this in def function
import numpy as np
def format_float(df):
cols=list(df.columns)
for col in cols:
df[col]=[np.format_float_positional(x) for x in df[col]]
return df
format_float(df)
outcome
a b
0 0.01 0.0000001
1 0.02 0.0000002
2 0.03 0.0000003