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
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())
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
first second third
A 0.3 0.8 0.4
B 0.557432 0.568175 0.745703
C 0.78 0.28 0.62
When transposing, it is a possibility that the now transposed rows (as columns) may not be of the same type, or of the same rounding. Pandas tries to remedy this (if possible, from a performance point of view), and so the rounding is reset. If you want to preserve the rounding, convert the dataframe to object
type, and then transpose -
df.astype(object).T
first second third
A 0.8 0.7 0.7
B 0.22444 0.475358 0.498084
C 0.17 0.87 0.71
Now, pandas makes no assumptions about object columns, and they are transposed as-is, without any attempt at transforming the data. Keep in mind that data as objects is suicidal in terms of performance, you might as well use python lists at this point, as objects do not offer any performance benefits.