Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I get an error when using .astype() with pandas

I get an error when trying to make a new df:

ERROR:

'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'

This is the code that gives the error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df = pd.DataFrame(data)
new_df = df
bools = ['ind-debateclub', 'ind-programming_exp', 
'ind-international_exp',
'ind-entrepeneur_exp', 'ind-exact_study', 'decision']

for i in bools:
    new_df[i] = df[i].astype(int)
new_df.head()

I have tried it like this, but this yields the same error:

new_df[i] = df.loc[df[i].astype(int)]

The input looks like this:
DataFrame

>Solution :

Try changing the new_df = df line to:

new_df = df.copy()

also change the .astype() line to:

new_df[i] = new_df[i].astype(int)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading