ML LAB
ML LAB

Reputation: 23

Pandas Series fill NaNs upto a certain limit only

I have a dataset "artwork.csv" https://gitlab.com/IEA_ML_LAB/test/-/blob/80713d4823c4778d11468bcaf4a5223f6a160c88/artwork.csv

The 'year' column includes int64 and NaN.

enter image description here

I want to replace the first 100 NaN values with text 'no date'. I tried different methods but didn't succeed.

'year' columns has 1279 NaN values. I want to set the first 100 out of 1279 to 'no date'

enter image description here

The first 100 NaN values:

enter image description here

I try the below commands. They don't produce any errors but they don't modify the Series either:

df.loc[df.year.isnull(), 'year'].iloc[:100] = 'no date'
(df.loc[df.year.isnull(), 'year'].iloc[:100]).replace('NaN', 'no date', inplace=True)
(df.loc[df.year.isnull(), 'year'].iloc[:100]).transform(lambda x: 'no date')

Thanks in advance.

Upvotes: 1

Views: 329

Answers (1)

cs95
cs95

Reputation: 402952

fillna has a limit argument you can set to 100:

df['year'] = df['year'].fillna('no date', limit=100)

No need to call iloc beforehand, as that would generate an additional copy of data.

Although beware mixing strings and floats is probably not the best option here as it severely impacts performance while handling the data.

Upvotes: 2

Related Questions