Divya Singhal
Divya Singhal

Reputation: 11

Replace values with nan in python

I have to set the values of the first 3 rows of dataset in column "alcohol" as NaN

newdf=pd.DataFrame({'alcohol':[np.nan]},index=[0,1,2])
wine.update(newdf)
wine

After running the code, no error is coming and dataframe is also not updated

Upvotes: 1

Views: 3113

Answers (2)

jezrael
jezrael

Reputation: 863791

Use iloc with get_loc for position for column alcohol:

wine.iloc[:3, wine.columns.get_loc('alcohol')] = np.nan

Or use loc with first values of index:

wine.loc[wine.index[:3], 'alcohol'] = np.nan

Upvotes: 0

Srce Cde
Srce Cde

Reputation: 1824

Assuming alcohol as column.

df.loc[:2, "alcohol"] = np.nan

#alternative
df.alcohol.iloc[:3] = np.nan

Upvotes: 1

Related Questions