How can one modify the format for the output from a groupby operation in pandas that produces scientific notation for very large numbers?
I know how to do string formatting in python but I’m at a loss when it comes to applying it here.
JavaScript
x
7
1
df1.groupby('dept')['data1'].sum()
2
3
dept
4
value1 1.192433e+08
5
value2 1.293066e+08
6
value3 1.077142e+08
7
This suppresses the scientific notation if I convert to string but now I’m just wondering how to string format and add decimals.
JavaScript
1
2
1
sum_sales_dept.astype(str)
2
Advertisement
Answer
Granted, the answer I linked in the comments is not very helpful. You can specify your own string converter like so.
JavaScript
1
9
1
In [25]: pd.set_option('display.float_format', lambda x: '%.3f' % x)
2
3
In [28]: Series(np.random.randn(3))*1000000000
4
Out[28]:
5
0 -757322420.605
6
1 -1436160588.997
7
2 -1235116117.064
8
dtype: float64
9
I’m not sure if that’s the preferred way to do this, but it works.
Converting numbers to strings purely for aesthetic purposes seems like a bad idea, but if you have a good reason, this is one way:
JavaScript
1
7
1
In [6]: Series(np.random.randn(3)).apply(lambda x: '%.3f' % x)
2
Out[6]:
3
0 0.026
4
1 -0.482
5
2 -0.694
6
dtype: object
7