Pandas Dataframe nan 值不替换 [英] Pandas Dataframe nan values not replacing
问题描述
试图替换我的数据框中列为nan"的值(注意,不是NaN")
Trying to replace values in my data frame which are listed as 'nan' (note, not 'NaN')
我读过一个 excel 文件,然后尝试像这样替换 nan 值:
I've read in an excel file, then tried to replace the nan values like this:
All_items_df = ALL_df[df_items].fillna(' ')
最后我得到一个仍然包含 'nan' 的输出
Finally I get an output that still contains 'nan'
All_items_df ['Colour'].head(10)
Out[]:
7 nan
8 nan
9 nan
10 nan
13 nan
14 nan
15 nan
16 nan
18 nan
19 nan
Name: Colour, dtype: object
使用 isna() 或 isnull().value.all() 检查 nan 值给我 False 上面的值.为什么它不能识别为 nan/na 值?
Checking the nan values using isna() or isnull().value.all() gives me False for the above values. Why is it not recognising as nan/na values?
All_items_df ['Colour'].isnull().head(10)
Out[123]:
7 False
8 False
9 False
10 False
13 False
14 False
15 False
16 False
18 False
19 False
Name: Minor Feats, dtype: bool
然后我正在写入一个 csv 文件并将nan"写入文件,即使指定不写出 nan
I'm then writing to a csv file and getting the 'nan' written to the file, even when specifying not to write out nan
All_items_df.to_csv(folderpath + "All_items.csv",encoding="UTF-8", index=False, na_rep='')
推荐答案
确保将 nan
值读为 NaN
.您可以通过 pd.read_excel
中的参数执行此操作:
Make sure you read your nan
values as NaN
. You can do this via a parameter in pd.read_excel
:
df = pd.read_excel('file.xlsx', na_values=['nan'])
奇怪的是,默认情况下 nan
不被视为 pd.read_excel
:
Strangely, by default nan
is not considered a NaN
value in pd.read_excel
:
na_values : 标量、str、类列表或字典,默认无
na_values : scalar, str, list-like, or dict, default None
要识别为 NA/NaN 的其他字符串.如果 dict 通过,则具体每列 NA 值.默认情况下解释以下值作为 NaN: ‘’, ‘#N/A’, ‘#N/A N/A’, ‘#NA’, ‘-1.#IND’, ‘-1.#QNAN’, ‘-NaN’,‘-nan’,
Additional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. By default the following values are interpreted as NaN: ‘’, ‘#N/A’, ‘#N/A N/A’, ‘#NA’, ‘-1.#IND’, ‘-1.#QNAN’, ‘-NaN’, ‘-nan’,
这篇关于Pandas Dataframe nan 值不替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!