0

How to round off column number - two(beta) and three(gama) to two decimal places for this dataframe

df

  alpha      beta      gama  theta
4.615556  4.637778  4.415556    4.5
3.612727  3.616364  3.556364    5.5
2.608000  2.661333  2.680000    7.5
2.512500  2.550000  2.566250    8.0
Lukas S
  • 3,212
  • 2
  • 13
  • 25
user14498621
  • 11
  • 1
  • 4

2 Answers2

4

You can use the .round function.

Here is an example program:

# Import pandas
import pandas as pd

# Example data from question
alpha = [4.615556, 3.612727, 2.608000, 2.512500]
beta = [4.637778, 3.616364, 2.661333, 2.550000]
gamma = [4.415556, 3.556364, 2.680000, 2.566250]
theta = [4.5, 5.5, 7.5, 8.0]

# Build dataframe
df = pd.DataFrame({'alpha':alpha, 'beta':beta, 'gamma':gamma, 'theta':theta})

# Print it out
print(df)

# Use round function
df[['beta', 'gamma']] = df[['beta', 'gamma']].round(2)

# Show results
print(df)

Yields:

      alpha      beta     gamma  theta
0  4.615556  4.637778  4.415556    4.5
1  3.612727  3.616364  3.556364    5.5
2  2.608000  2.661333  2.680000    7.5
3  2.512500  2.550000  2.566250    8.0

      alpha  beta  gamma  theta
0  4.615556  4.64   4.42    4.5
1  3.612727  3.62   3.56    5.5
2  2.608000  2.66   2.68    7.5
3  2.512500  2.55   2.57    8.0
artemis
  • 6,857
  • 11
  • 46
  • 99
1
df[['beta','gama']] = df[['beta','gama']].round(2)
Lukas S
  • 3,212
  • 2
  • 13
  • 25
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality – Fabio Veronese Oct 22 '20 at 13:57