Home > Mobile >  how to let one column keep two decimal point, another column` without decimal point(int) in datafram
how to let one column keep two decimal point, another column` without decimal point(int) in datafram

Time:12-10

Supposed I have both sum and mean value in dataframe, how to let difference column keep two decimal point, and value_a and value_b keep int(without decimal point)

df.columns=['value_a','value_b','name','up_or_down','difference']

df1 = df.agg({'value_a':'sum', 'value_b':'sum', 'difference':'mean'}).to_frame('Total').T

df = pd.concat([df1,df])
print (df.head())
                value_a  value_b  difference            name up_or_down
Total             27.56    25.04   -0.035405             NaN        NaN
2021-project11     1.43     3.48    0.05  2021-project11         up
2021-project1      2.62     3.56   -0.06   2021-project1       down
2021-project2      5.51     2.47   -0.3   2021-project2       down
2021-porject3      5.37     2.34   -0.12   2021-porject3       down

My except output is as below:

                value_a  value_b  difference            name up_or_down
Total             13    10   -0.035            NaN        NaN
2021-project11     1     3    0.05  2021-project11         up
2021-project1      2     3   -0.06   2021-project1       down
2021-project2      5     2   -0.30   2021-project2       down
2021-porject3      5     2   -0.12   2021-porject3       down

CodePudding user response:

Use DataFrame.astype and for 2 decimals DataFrame.round, in your expected output data are 3 decimals in first row, it is not possible, need same decimal in column:

df = df.astype({'value_a':int, 'value_b':int})
df1 = df.agg({'value_a':'sum', 'value_b':'sum', 'difference':'mean'}).to_frame('Total').T
df = pd.concat([df1,df]).round({'difference': 2})
print (df)
  • Related