AttributeError: 'DataFrame' object has no attribute 'ix' error occurs when writing Python code

When writing the following python code, an error occurs: AttributeError: 'DataFrame' object has no attribute 'ix'

fdata.ix[fdata['time']=='Diner','time']='Dinner'
fdata.ix[fdata['time']=='Dier','time']='Dinner'
fdata['time'].unique()

Error screenshot:

Please add image description
Chinese meaning: "DataFrame" object has no attribute "ix"

The reason is that the 'ix' attribute of pandas has been deprecated in the latest version.
The new version of pandas has upgraded and refactored this function. You can use the ( 'loc'–>can represent rows and 'iloc'---->can represent columns ) attributes instead of 'ix', both of which can be used For selecting rows and columns in a DataFrame.
So just change " ix " to " loc " to solve the above problem.

fdata.loc[fdata['time']=='Diner','time']='Dinner'
fdata.loc[fdata['time']=='Dier','time']='Dinner'
fdata['time'].unique()

Output result:

Please add image description

Guess you like

Origin blog.csdn.net/qq_62127918/article/details/130806580