0

I have been trying to do some analysis on the Year column in the csv file, Since it's in object data type, I am trying to convert to float to carry forward my analyses.

Code##...

import pandas as pd
data=pd.read_csv(r"data1.csv",sep=None,engine='python')

Year    Length  Title   Subject Actor   Actress Director    

1   1990    111 Tie Me Up! Tie Me Down! Comedy  Banderas, Antonio   Abril, 
2   1991    113 High Heels  Comedy  Bosé, Miguel    Abril, Victoria Almodóvar, 
data.dtypes

Year          object
Length        object
Title         object
Subject       object
Actor         object
Actress       object

Then I use the below code to convert the "Year" column to float datatype.Though it's converted successful when calling the column "Year" alone, buy the result is reflected when I run the code" data.dtypes" again to check the datatypes of the column.

pd.to_numeric(data["Year"],errors='coerce')

1654    1990.0
1655    1932.0
1656    1989.0
1657    1988.0
1658    1977.0
1659    1991.0
Name: Year, dtype: float64

data.dtypes

Year          object
Length        object
Title         object
Subject       object
Actor         object
Actress       object

The conversion happened only for the column at that moment and not reflected and saved in the table

How to convert the column's data type object to float and save it in the table as it can be used for further analyses.

2 Answers 2

1

You need to assign the result of your conversion:

data["Year"] = pd.to_numeric(data["Year"],errors='coerce')
Sign up to request clarification or add additional context in comments.

Comments

1

You did not assign the change made. Please see the return types and down casting in the link below:

Pandas Documentation for to_numeric

Comments

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.