Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I have this code here, but I get errors for trying to make it Float when I use Jupyter Notebook for the data science

Hello,

I have this code here, but I get errors for trying to make it Float when I use Jupyter Notebook for the data science course.

I need to use the astype() function of the DataFrame to convert these MPG columns to float (see coding below).

new_df['City MPG'] = new_df['City MPG'].astype("float")new_df['Hwy MPG'] = new_df['Hwy MPG'].astype(float)new_df['Cmb MPG'] = new_df['Cmb MPG'].astype(float)

It says this:

It says: A value is trying to be set on a copy of a slice from a DataFrame.Try using .loc[row_indexer,col_indexer] = value instead

But I don't know how to edit my code to their requirement, and I would like your help with this.

Here is the screenshot of the original error.

image text in transcribed
In [49] : 1 new df [ 'City MPG' ] = new_df [ 'City MPG' ] . astype ( "float") 2 new df [ ' Hwy MPG' ] = new_df [ 'Hwy MPG' ] . astype (float) W new_df [ ' Cmb MPG' ] = new_df [ ' Cmb MPG' ] . astype (float) :1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using . loc[row_indexer, col_indexer] = value instead See the caveats in the documentation: https: / /pandas . pydata.org/pandas-docs/stable/user_guid e/indexing . html#returning-a-view-versus-a-copy new_df [ 'City MPG' ] = new_df [ 'City MPG' ] . astype ( "float") :2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using . loc[row_indexer, col_indexer] = value instead See the caveats in the documentation: https : / /pandas . pydata. org/pandas-docs/stable/user_guid e/indexing . html#returning-a-view-versus-a-copy new_df [ 'Hwy MPG' ] = new_df [ 'Hwy MPG' ] . astype (float) :3: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using . loc[row_indexer, col_indexer] = value instead See the caveats in the documentation: https: / /pandas . pydata. org/pandas-docs/stable/user_guid e/indexing . html#returning-a-view-versus-a-copy new_df [ ' Cmb MPG' ] = new_df [ 'Cmb MPG' ] . astype (float)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions