According to the pandas.DataFrame.round
documentation I can decide the rounding also column-wise. However, there is nothing written about a row-wise rounding. For instance, I have
A
count 1010.00009
measure 54.45678
average 0.50483
How could I make it that
A
count 1010
measure 54.46
average 0.5048
When something supports columns but not the index, your only option is to transpose... twice.
df.T.round(decimals=dict(zip(df.index, [0, 2, 4]))).T
Or,
df.T.round(decimals=pd.Series([0, 2, 4], index=df.index)).T
Adding a .astype(object)
call at the end, we have—
A
count 1010
measure 54.46
average 0.5048
With zip
and round
(from python), and for print I am adding astype(object)
df.A=[round(x,y) for x , y in zip(df.A,[0,2,4])]
df.astype(object)
A
count 1010
measure 54.46
average 0.5048
我们转置 DataFrame,然后将列舍入并再次转置。
df = df.T.round( { "row_name": 0, "row2" : 2, "row3" : 4 } ).T
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.