I am defining decimal points carefully by column in a large results table, but when I transpose it so it can fit on a page in my report (using .to_latex
), pandas sets the decimals to whatever the largest number is.
Is it possible to keep my decimal precision during the transpose? I would rather not have to define the decimals after the transpose.
(NOTE: I have noticed it does not do this when there is a row of strings in the dataframe before the transpose, so I am thinking it is some kind of datatype definition that I am not understanding?)
Example code:
df = pd.DataFrame(np.random.random([3, 3]),
columns=['A', 'B', 'C'], index=['first', 'second', 'third'])
df = df.round({'A': 1, 'C': 2})
print(df)
print(df.transpose())
Output:
A B C
first 0.3 0.557432 0.78
second 0.8 0.568175 0.28
third 0.4 0.745703 0.62
first second third
A 0.300000 0.800000 0.400000
B 0.557432 0.568175 0.745703
C 0.780000 0.280000 0.620000
I want:
first second third
A 0.3 0.8 0.4
B 0.557432 0.568175 0.745703
C 0.78 0.28 0.62