Nicole Andrew Nicole Andrew - 2 months ago 32
LaTeX Question

Pandas transpose resets decimal rounding

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

), 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})


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

Answer Source

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 -


     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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download