Skip to content Skip to sidebar Skip to footer

Extract Floats From A Column Of Strings And Round To 2 Decimal Places

If i have a data frame with values in a column 4.5678 5 7.987.998 I want to extract data for only 2 values after the decimal 4.56 5 7.98 The data is stored as a string. Any help

Solution 1:

print(s) 
04.56781527.987.998
Name: 0, dtype: objectprint(type(s))
Out[152]: pandas.core.series.Series

Using str.extract + round:

r = s.str.extract('(\d+(?:\.\d+)?)', \
           expand=False).astype(float).round(2)
print(r)
04.5715.0027.99
Name: 0, dtype: float64

Unfortunately, the 5 cannot be an integer as your expected output describes, that would lead to mixed types and is generally discouraged.

Solution 2:

str = "7.987.998"
ind = str.find('.')
if ind > 0:
  res = str[:ind+3]

Solution 3:

defget_two_spaces(input):
    input_list = input.split('.')
    iflen(input_list) >= 2:
        return input_list[0] + '.' + input_list[1][:2]
    returninput

I'll break down what's happening here:

  1. we split the string into a list of strings around the period character.
  2. we see how many items are in that list:
  3. if there are 2 or more, we return the entire first string, a period, and the first 2 characters of the second string
  4. if there are not, we just return the original input.

Solution 4:

Another pandas approach:

import pandas as pd

df = pd.DataFrame(['4.5678','5','7.987.998'], columns=['A'])
s = df['A'].replace(to_replace='^(\d+\.\d+)\.\d+', value=r'\1', regex=True)\
    .astype('float').map('{:,.2f}'.format)

print(s)

The output:

04.5715.0027.99Name: A, dtype: object

Post a Comment for "Extract Floats From A Column Of Strings And Round To 2 Decimal Places"