-3

I have data like:

enter image description here

I want to make all numerical values to int, no decimal will be here. like this:

enter image description here

I was using code like list:

             df=pd.read_csv("file.csv")

             df = df.astype('int64')

But it was not working. Because it was saying:

          ValueError: Cannot convert NA to integer

Because, I have some nan value inside. There are string also in the column one row 2&3. I think the solution could be, selecting all numerical values from the data frame and converting to int. Can you suggest anything?

Tashaho
  • 163
  • 1
  • 2
  • 11

1 Answers1

-1

There are some null values in your data frame. First, you need to covert them to an integer (for e.g. 0 here). use df.fillna for that. Then select all numerical columns.

cols = ["num_col1", "num_col2"]
df[cols] = df[cols].astype("int64")
Rahul Goswami
  • 535
  • 3
  • 14
  • Where does it say 0 is NA? – pyeR_biz Jun 22 '18 at 22:48
  • Not a great answer to an especially poor question. – cs95 Jun 22 '18 at 22:53
  • Wait, am I the only one to understand OPs question? It has also been marked as wrong duplicate. Its more close (not exactly) to [this question](https://stackoverflow.com/questions/25039626/how-do-i-find-numeric-columns-in-pandas). – Rahul Goswami Jun 23 '18 at 04:30