[英]Pandas Series fill NaNs upto a certain limit only
我有一個數據集“artwork.csv” https://gitlab.com/IEA_ML_LAB/test/-/blob/80713d4823c4778d11468bcaf4a5223f6a160c88/artwork.Z628CB5675FF524F3E7ZEFE88FAA73
“年份”列包括 int64 和 NaN。
我想用文本“無日期”替換前 100 個 NaN 值。 我嘗試了不同的方法,但沒有成功。
'year' 列有 1279 個 NaN 值。 我想將 1279 中的前 100 個設置為“無日期”
前 100 個 NaN 值:
我嘗試以下命令。 它們不會產生任何錯誤,但它們也不會修改系列:
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')
提前致謝。
fillna
有一個可以設置為 100 的limit
參數:
df['year'] = df['year'].fillna('no date', limit=100)
無需事先調用iloc
,因為這會生成額外的數據副本。
雖然要注意混合字符串和浮點數可能不是最好的選擇,因為它在處理數據時會嚴重影響性能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.