0

I read data from a CSV file and almost all columns are of the object type. How can I convert them into int. I tried several options but without success. The CSV file is in image. I am especially interested in columns latitude and longitude.

CSV File

df = pd.read_csv('file'.csv)
de.head()

df.dtypes
listings       object
url            object
guests         object
bedrooms        int64
beds            int64
bathrooms       int64
room_type      object
price         float64
host_name      object
reviews         int64
stars         float64
location       object
latitude       object
longitude      object
dtype: object
2
  • 3
    Have you tried df.longitude = df.longitude.astype(int)? Commented May 7, 2020 at 14:11
  • How about: df['longitude'] = df['longitude'].astype(str).astype(int)? Commented May 7, 2020 at 14:13

2 Answers 2

1

you can us .astype(), but perhaps need to do to str first then to int

df["yourColumn"] = df["yourColumn"].astype(str).astype(int)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the below code to change...

df['longitude'].to_numeric()

1 Comment

but wont it take too long to do every column one by one isnt there any other way to do it fast

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.