Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I have a pandas DataFrame object named xiv which has a column of int64 Volume measurements.

In[]: xiv['Volume'].head(5)

Out[]: 

0    252000

1    484000

2     62000

3    168000

4    232000

Name: Volume, dtype: int64

I have read other posts (like this and this) that suggest the following solutions. But when I use either approach, it doesn't appear to change the dtype of the underlying data:

In[]: xiv['Volume'] = pd.to_numeric(xiv['Volume'])

In[]: xiv['Volume'].dtypes

Out[]: 

dtype('int64')

Or...

In[]: xiv['Volume'] = pd.to_numeric(xiv['Volume'])

Out[]: ###omitted for brevity###

In[]: xiv['Volume'].dtypes

Out[]: 

dtype('int64')

In[]: xiv['Volume'] = xiv['Volume'].apply(pd.to_numeric)

In[]: xiv['Volume'].dtypes

Out[]: 

dtype('int64')

I've also tried making a separate pandas Series and using the methods listed above on that Series and reassigning to the x['Volume'] obect, which is a pandas.core.series.Series object.

I have, however, found a solution to this problem using the numpy package's float64 type - this works but I don't know why it's different.

In[]: xiv['Volume'] = xiv['Volume'].astype(np.float64)

In[]: xiv['Volume'].dtypes

Out[]: 

dtype('float64') 

Can someone explain how to accomplish with the pandas library what the numpy library seems to do easily with its float64 class; that is, convert the column in the xiv DataFrame to a float64 in place.

1 Answer

0 votes
by (41.4k points)

You can use this:

pd.to_numeric(df.valueerrors='coerce').fillna(0, downcast='infer')  

It will use zero in place of nan.

If you want to Learn  What is Python then visit this Python Course by Intellipaat.

Browse Categories

...