I have four columns of type object
in a Pandas (2.0.1) DataFrame which want to convert to int
.
Applying the following method:
cols = ['x1','x2','y1','y2']
df[cols] = df[cols].apply(pd.to_numeric)
# The same message is raised when trying to cast a single column:
df['x1'] = pd.to_numeric(df['x1'])
# The same message is also raised when using .astype():
dff[cols] = dff[cols].astype(int)
as described here: https://stackoverflow.com/a/28648923/6630397 raises the message:
/tmp/ipykernel_87959/2834796204.py: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_guide/indexing.html#returning-a-view-versus-a-copy
df[cols] = df[cols].apply(pd.to_numeric)
How can I properly (and rapidly) cast my four columns from object
to int
?