1

I have a column with numbers. It has about 60000 indexes. Some indexes have values like 1.000 or 5.0 or 2323.0000 or 1.446 The correct value for these indexes are 1, 5, 2323, 1446. In other words, i have two cases. For first case, if a index is numeric value with dot and zeros after that dot, i need to remove the dot and all the zeros after the dot. Second case is when a index has a numeric value with dot, but number after dot is not zero. i Just need to get rid of the dot. How would i accomplish this?

Godseph
  • 31
  • 7

3 Answers3

2

I think you need:

df = pd.DataFrame({'a':range(5)}, index=[1.0,5.0,2323.0,1.446,10.5])
print (df)
          a
1.000     0
5.000     1
2323.000  2
1.446     3
10.500    4

df.index = df.index.map(lambda x: int(x) if x.is_integer() else int(x * 1000))

Or:

df.index = np.where(df.index.astype(int) == df.index,df.index, df.index * 1000).astype(int)

print (df)
       a
1      0
5      1
2323   2
1446   3
10500  4

Or maybe need:

df.index = df.index.astype(str).str.replace('\.0','').str.replace('.','').astype(int)
print (df)
      a
1     0
5     1
2323  2
1446  3
105   4
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

To check if the value is integer already check How to check if a float value is a whole number

If it is not you can just multiply by then until it is. Afterwards you can cast to int in both cases.

0

this may not be the most elegant solution, but it's simple and clean, no imports and stuff, just basic python

outcome = []
for i in raw:
    if i%1 == 0: # check if number is whole
        outcome += int(i) # append normally as int
    else:
        outcome += int(str(i).replace('.','')) #replace .
return outcome

which could be

return [(int(i) if i.is_integer() else int(str(i).replace('.',''))) for i in raw]
Sadru
  • 46
  • 1
  • 9