Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to let one column keep two decimal point, another column` without decimal point(int) in dataframe/Pandas

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading