Reputation: 363
part of of my crosstab, I figured out how to re order the index but now I am stuck on how to format my crosstab output to show the data to the 2nd. decimal point.
I started with this code
pd.crosstab(data['One'],data['two'], margins=True).apply(lambda r: r/len(data)*100,axis = 1)
The out put looks like this
A B C All
B 10.0000 40.0000 50.0000
C 40.0000 10.0000 50.0000
All 50.0000 50.0000 100.0000
but im looking for something like
A B C All
B 10.00% 40.00% 50.00%
C 40.00% 10.00% 50.00%
All 50.00% 50.00% 100.00%
Any ideas on how to format my data? Thanks again.
Upvotes: 1
Views: 2398
Reputation: 806
your lambda is the main doing the main work in that line therefore i have done some changes
pd.crosstab(data['One'],data['two'], margins=True).apply(lambda r:"{:.2f}".format((r/len(data)*100))+'%',axis = 1)
Upvotes: 0
Reputation: 19634
You can do
pd.options.display.float_format = '{:.2f}%'.format
Then if you display the dataframe it will have only two digits after the decimal point with % sign.
Upvotes: 6