0

I need to remove the .0s after floats in a column of a DataFrame, made from a dictionary.

For example, the dictionary might be:

mydict = { "part1" : [1,2,None,4,5] "part2" : [6,7,None,9,10] }

and then when I mydf = pd.DataFrame(mydict), the DataFrame generated is as follows:

part1 part2
0 1.0 6.0
1 2.0 7.0
2 NaN NaN
3 4.0 9.0
4 5.0 10.0

This happens because every single column in a DataFrame must have all objects of the same type. But, I want to have no .0s at the end of my data for look purposes. Obviously, I can't make them integers, due to the lack of an NaN in integers. I also can't make them strings, for the reason of numerical sorting. I also wouldn't want "01","02","03"…"10" for the purpose of looks.

Becuase this project is really serious, the looks matter, so please don't blame me of overthinking looks of data.

  • You can find this answer here: https://stackoverflow.com/questions/21291259/convert-floats-to-ints-in-pandas – Senne Dec 31 '22 at 01:22
  • 1
    Would `pd.set_option('precision', 0)` work for you? See also this question: https://stackoverflow.com/questions/37084812/how-to-remove-decimal-points-in-pandas – Nick ODell Dec 31 '22 at 01:22

1 Answers1

0

the comments point out a couple of solutions.

  • i prefer to cast to type .astype('Int64') which retains the NaN as a <NA>.

here is my solution (with your data):

import pandas as pd

mydict = {"part1":[1,2,None,4,5],  "part2":[6,7,None,9,10]}

df = pd.DataFrame(mydict)


df['part2'] = df['part2'].astype('Int64')
print(df)

returns this:

   part1  part2
0    1.0      6
1    2.0      7
2    NaN   <NA>
3    4.0      9
4    5.0     10

You can apply the above to one (or many) columns of your choice.

D.L
  • 4,339
  • 5
  • 22
  • 45