Pandas系列只在一定限度内填充NAN

2024-04-24 14:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集“artwork.csv” https://gitlab.com/IEA_ML_LAB/test/-/blob/80713d4823c4778d11468bcaf4a5223f6a160c88/artwork.csv

“年份”列包括int64和NaN

enter image description here

我想用文本“无日期”替换前100个NaN值。我尝试了不同的方法,但没有成功

“年”列有1279个NaN值。我想将1279中的前100个设置为“无日期”

enter image description here

前100个NaN值:

enter image description here

我尝试下面的命令。它们不会产生任何错误,但也不会修改序列:

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')

提前谢谢


Tags: csv数据nohttpscomdfdategitlab
1条回答
网友
1楼 · 发布于 2024-04-24 14:32:17

^{}有一个limit参数,可以设置为100:

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

无需事先调用iloc,因为这将生成额外的数据副本

尽管在这里,混合字符串和浮点可能不是最好的选择,因为它在处理数据时会严重影响性能

相关问题 更多 >