英文:
Pandas treat 'None' value as NaN in one column and as 'None' in other column...?
问题
| Fluor | Comment |
| 强 | 无 |
| 无 | '无' |
| 无 | 是 |
我有一个 CSV 文件,当在 Pandas 中读取它时,Fluor 列的 "无" 被视为 NaN,但 Comment 列的 "无" 被视为 '无'。
我不理解这种行为,我想将 "无" 视为字符串。
请指导我。
英文:
Fluor | Comment |
---|---|
Strong | None |
None | None |
None | Yes |
I have a csv file, while reading it in pandas, Fluor column's None treat as NaN but Comment column's None treat as 'None'.
I don't understand the behavior, I want to treat None as string.
Please guide me.
答案1
得分: 0
我无法复现您描述的某一列行为不同的问题。
然而,默认情况下,read_csv
将None
视为NaN
,如果您想要阻止这种情况,可以传递一个显式的na_values
列表:
allowed_nans = ['', '#N/A', '#N/A N/A', '#NA', '-1.#IND', '-1.#QNAN',
'-NaN', '-nan', '1.#IND', '1.#QNAN', '<NA>', 'N/A', 'NA',
'NULL', 'NaN', 'n/a', 'nan', 'null']
df = pd.read_csv('filename.csv', na_values=allowed_nans, keep_default_na=False)
print(df)
这应该使您能够将None
作为字符串处理。
请注意,我在此处列出了除了None
之外的所有默认值,您当然可以只保留对您相关的值。
您也可以避免所有的NaN:
df = pd.read_csv('filename.csv', keep_default_na=False)
输出:
Fluor Comment
0 Strong None
1 None None
2 None Yes
英文:
I can't reproduce your issue in which one columns behaves differently.
However, by default, read_csv
treats None
as NaN
, if you want to prevent this, pass an explicit list of na_values
:
allowed_nans = ['', '#N/A', '#N/A N/A', '#NA', '-1.#IND', '-1.#QNAN',
'-NaN', '-nan', '1.#IND', '1.#QNAN', '<NA>', 'N/A', 'NA',
'NULL', 'NaN', 'n/a', 'nan', 'null']
df = pd.read_csv('filename.csv', na_values=allowed_nans, keep_default_na=False)
print(df)
This should enable you to have None as a string.
Note that I'm listing here all default values except for None
, you can of course only keep those that are relevant to you.
You can also avoid all NaNs:
df = pd.read_csv('filename.csv', keep_default_na=False)
Output:
Fluor Comment
0 Strong None
1 None None
2 None Yes
答案2
得分: 0
你可以使用replace函数将所有的NaN和None替换为字符串"None"。这是一个非常模糊的解决方案,但会有所帮助。
df = df.replace("NaN", "None")
df = df.replace("None", "None")
df = df.replace(np.nan, "None")
# 或者使用任何其他形式的NaN或None。
英文:
You can replace all the NaN and None to string "None" using the replace function. This is very vague solution but will help.
df = df.replace("NaN", "None")
df = df.replace("None", "None")
df = df.replace(np.nan, "None")
# or any other form of NaN or None.
答案3
得分: 0
我观察到使用 pandas==2.0.0 时,我遇到了上述问题,但在 pandas==1.5.3 中,它会将空单元格视为 NaN,将 'None' 视为 'None'(通常的行为)。
谢谢所有回应的人。
英文:
I observed that with pandas==2.0.0 I'm facing above problem, but with pandas==1.5.3 treat Blank Cells as NaN and 'None' as 'None'(the usual behavior).
Thanks to all who responded.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论