2

I have data frame with a object column lets say col1, which has values likes: 1.00, 1, 0.50, 1.54

I want to have the output like the below: 1, 1, 0.5, 1.54 basically, remove zeros after decimal values if it does not have any digit after zero. Please note that i need answer for dataframe. pd.set_option and round don't work for me.

Alexsander
  • 603
  • 1
  • 6
  • 15

5 Answers5

2

If want convert integers and floats numbers to strings with no trailing 0 use this with map or apply:

df = pd.DataFrame({'col1':[1.00, 1, 0.5, 1.50]})

df['new'] = df['col1'].map('{0:g}'.format)
#alternative solution
#df['new'] = df['col1'].apply('{0:g}'.format)
print (df)
   col1  new
0   1.0    1
1   1.0    1
2   0.5  0.5
3   1.5  1.5

print (df['new'].apply(type))
0    <class 'str'>
1    <class 'str'>
2    <class 'str'>
3    <class 'str'>
Name: new, dtype: object
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I am getting the below error message: ValueError: Unknown format code 'g' for object of type 'str' – Alexsander Oct 19 '18 at 09:36
  • jezrael,your above example work if the 'col1' is float. otherwise it through error message. my issue is with string (object) data type – Alexsander Oct 19 '18 at 09:51
  • @Alexsander - How working `df['new'] = df['col1'].astype(float).map('{0:g}'.format)` ? – jezrael Oct 19 '18 at 10:51
1

I think something like this should work:

if val.is_integer() == True :
    val = int(val)
elif val.is_float() == True :
    val = Decimal(val).normalize()

Assuming that val is a float value inside the dataframe's column. You simply cast the value to be integer. For float value instead you cut extra zeros.

A. Wolf
  • 1,309
  • 17
  • 39
0

A quick-and-dirty solution is to use "%g" % value, which will convert floats 1.5 to 1.5 but 1.0 to 1 and so on. The negative side-effect is that large numbers will be represented in scientific notation like 4.44e+07.

csl
  • 10,937
  • 5
  • 57
  • 89
  • CSI,thank you for the quick reply. since i am new to python. could you give me some example using dataframe, please? I never used "%g" % value before. – Alexsander Oct 19 '18 at 09:09
0

Taken from this Stackoverflow answer, I think you'd like to change the display precision of pandas like so:

pd.set_option('precision', 0)
Felix
  • 1,837
  • 9
  • 26
  • Good find, but this is a duplicate answer, not a separate. – Anton vBR Oct 19 '18 at 09:22
  • Thanks. Sorry I'm new to Stackoverflow. Is linking to a previous answer and citing parts of its content bad? Sorry but I couldn't find authoritative sources that define a duplicate answer. – Felix Oct 19 '18 at 09:32
  • No worries, I don't know if you have that option yet but there is a close question button at top where you can click towards --> duplicate answer. – Anton vBR Oct 19 '18 at 09:38
  • Thanks again. Seems like I'm still missing privileges. Also I just realized that my answer is wrong I think, because the question was referring to strings. Should I delete it? Or keep it for reference? – Felix Oct 19 '18 at 10:03
0

How about the str.rstrip method. Like so (assuming your strings are in a list):

a = ["1.00", "1" ,"0.50", "1.50"]

b = [e.rstrip('.0') for e in a]

>>> ['1', '1', '0.5', '1.5']
ragamuffin
  • 460
  • 2
  • 12
  • 1
    No, because it change all integers ended with `0` - `a = ["1.00", "100" ,"0.50", "1.50"]` create `['1', '1', '0.5', '1.5']` – jezrael Oct 19 '18 at 09:23