BhishanPoudel
BhishanPoudel

Reputation: 17144

Pandas dataframe replace digits after N decimals to empty string

I was learning how to using regex in pandas dataframe replace. I encountered the following problem:

I am trying to replace string after N decimal points to empty. E.g 12.349 ==> 12.35.

MWE

import numpy as np
import pandas as pd
import seaborn as sns

df1 = pd.DataFrame({'A': ['hello','wold'],
                   'B': [12.346789, 12.223344]})
df1 = df1.astype(str)
round_ = 2
to_replace = r"(^\d+\." + r"\d" * round_ + r")(.*)"
repl = lambda m: m.group(0)

df1 = df1.replace(to_replace,repl,regex=True)
df1

Pandas documentation says I can use regex to replace the string, but when I used that I got function repr instead of the value. How the problem can be solved?

Update

I was trying to apply format to transpose of dataframe. (Of course I can style before transform, but for some reasons I need to apply format to transpose).

df1 = pd.DataFrame({'A': ['hello','wold'],
                   'B': [12.349, 12.22]})
df1 = df1.T
df1.style.format({'B': "{:.2f}"}, axis=0)

References

Upvotes: 1

Views: 749

Answers (4)

LevB
LevB

Reputation: 953

Assuming you are starting with strings, you can convert the column into numbers and use round() to keep only 2 digits.

import pandas as pd

df = pd.DataFrame({'A': ['hello','wold'],
               'B': ['12.346789', '12.223344']})

df["B"] = round(pd.to_numeric(df["B"]),2)

print(df) 

Output:

A      B
0  hello  12.35
1   wold  12.22

If you already have a column of numbers, all you need is this.

df["B"] = round(df["B"],2)

Upvotes: 1

Andy L.
Andy L.

Reputation: 25239

I think you just need convert df1.B to float and use map with f-string

s = df1.B.astype(float).map(lambda x: f'{x:.02f}')

Out[8]:
0    12.35
1    12.22
Name: B, dtype: object

Upvotes: 2

Quang Hoang
Quang Hoang

Reputation: 150735

You can try this pattern:

df1 = df1.replace(r"^(\d+\.\d{," + rf"{round_}" + r'})(\d*)',r'\1',regex=True)

Output:

      survived pclass    age  sibsp  parch    fare
count    891.0  891.0  714.0  891.0  891.0   891.0
mean      0.38   2.30  29.69   0.52   0.38   32.20
std       0.48   0.83  14.52   1.10   0.80   49.69
min        0.0    1.0   0.42    0.0    0.0     0.0
25%        0.0    2.0  20.12    0.0    0.0    7.91
50%        0.0    3.0   28.0    0.0    0.0   14.45
75%        1.0    3.0   38.0    1.0    0.0    31.0
max        1.0    3.0   80.0    8.0    6.0  512.32

Upvotes: 4

ScootCork
ScootCork

Reputation: 3676

One way would be to multiply by 100, cast to int and divide by 100:

import pandas as pd

df1 = pd.DataFrame({'A': ['hello','wold'],
                   'B': [12.346789, 12.223344]})

df1['B'] = (df1.B * 100).astype(int) / 100

print(df1)

prints:

       A      B
0  hello  12.34
1   wold  12.22

Upvotes: 2

Related Questions