0

I have a dataframe with different values. The values are numbers, but sometimes they can be strings. I want to show only one decimal after the dot.

I've tried converting to string, but I don't know how to take one character after the dot.

This is my dataframe(reduced):

0   18.932202   19.063694   19.822535   22.3385
1   19.215377   19.424890   20.249221   22.3116
2   19.096696   19.361342   20.011814   21.2492
3   18.914138   19.016130   >30.0       22.3597
4   18.896591   19.051426   19.647521   20.982

the number with '>' is a string, that's why I convert everything to string.

Expected dataframe:

0   18.9    19.0    19.8    22.3
1   19.2    19.4    20.2    22.3
2   19.0    19.3    20.0    21.2
3   18.9    19.0    >30.0   22.3
4   18.8    19.0    19.6    20.9
Ipa
  • 109
  • 1
  • 8

6 Answers6

1

Use astype() and round()

This is a sample with several types

data = [[18.932202,19.063694,'19.822535',22.3385],
        [19.215377,19.424890 ,'20.249221',22.3116],
        [19.096696,19.361342,'20.011814',21.2492],
        [18.914138,'19.016130',9.734784 ,22.3597],
        [18.896591,19.051426,19.647521 ,20.982]]

df = pd.DataFrame(data, columns=['c1', 'c2', 'c3', 'c4'])
df.dtypes

Output :

c1    float64
c2     object
c3     object
c4    float64
dtype: object

Fistly you need to cast your Dataframe with astype() on float64 (type of numbers with dot)

df = df.astype('float64')
df.dtypes

Output :

c1    float64
c2    float64
c3    float64
c4    float64
dtype: object

Then use round() to reduce numbers after dot

df = df.round(1)
df

Output

    c1      c2      c3      c4
0   18.9    19.1    19.8    22.3
1   19.2    19.4    20.2    22.3
2   19.1    19.4    20.0    21.2
3   18.9    19.0    9.7     22.4
4   18.9    19.1    19.6    21.0
Demont Zhang
  • 184
  • 4
0

Use regular expressions?

Here is one that would do the trick - I assume you want the whole string and then one digit after the dot is encountered:

.*\d+\.\d

Hope it helps!

Edit: you can do something like the following

df
       0          1          2        3
0  18.932202  19.063694  19.822535  22.3385
1  19.215377  19.424890  20.249221  22.3116
2  19.096696  19.361342  20.011814  21.2492

import re
pattern = re.compile('.*\d+\.\d')
df.applymap(lambda x: pattern.match(str(x)).group(0))

      0     1     2     3
0  18.9  19.0  19.8  22.3
1  19.2  19.4  20.2  22.3
2  19.0  19.3  20.0  21.2
Aleks J
  • 283
  • 1
  • 9
0

Maybe u can use applymap

df1 = df.applymap(lambda x:x[0:4])

for example

df = pd.DataFrame({'a':['>1.1111','<1.1222'],'b':['>1.23333','18.1111']})

        a         b
0  >1.1111  >1.23333
1  <1.1222   18.1111

df1 = df.applymap(lambda x:x[0:4])

      a     b
0  >1.1  >1.2
1  <1.1  18.1
xiutiqianshi
  • 209
  • 1
  • 10
0

Is this what you are looking for?:

def myprecision(input):
    ret = ""
    mysize = len(input)
    j=-1
    for i in range(0,mysize):
        if input[i] == '.':
            j = i+1
            break
    ret = input[:j+1]
    return ret


a = ">3.14159"
b = myprecision(a)
print(b)    
# output is = >3.1
Qazi Fahim Farhan
  • 2,066
  • 1
  • 14
  • 26
0

With rounding

values = ["19.063694", ">9.734784"]
newValues = []

for value in values:
    if value.startswith('>'):
        newValues.append(">%.1f" % float(value.lstrip('>')))
    else:
        newValues.append("%.1f" % float(value))

print(newValues) # ['19.1', '>9.7']

Without rounding

def truncate(f, n): # https://stackoverflow.com/a/783927/3209393
    '''Truncates/pads a float f to n decimal places without rounding'''
    s = '%.12f' % f
    i, p, d = s.partition('.')
    return '.'.join([i, (d+'0'*n)[:n]])

values = ["19.063694", ">9.734784"]
newValues = []

for value in values:
    if value.startswith('>'):
        newValues.append('>' + truncate(float(value.lstrip('>')), 1))
    else:
        newValues.append(truncate(float(value), 1))

print(newValues) ['19.0', '>9.7']
Christopher Graf
  • 1,929
  • 1
  • 17
  • 34
0

My solution:

round(df.replace(">30.0",10000),1).replace(10000,">30.0")

I've changed all the strings with ">" to a large float and then round all off them, at the end I replace the large float for the same string.

Ipa
  • 109
  • 1
  • 8