Convert a column from pandas dataframe from float to int

Viewed 8867

When I want to do the following command(learned from the other question), there is a warning. How to avoid this warning?

df['Class'] = df['Class'].astype(int)

/home/ubuntu/src/anaconda3/lib/python3.5/site-packages/ipykernel/main.py: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: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy from ipykernel import kernelapp as app

2 Answers

You need to disable chained assignments.

# Disable chained assignments
pd.options.mode.chained_assignment = None 

Will make it work.

Related