Pandas将一个列中的’None’值视为NaN,而在另一个列中视为’None’…?

huangapple go评论71阅读模式
英文:

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_csvNone视为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 = [&#39;&#39;, &#39;#N/A&#39;, &#39;#N/A N/A&#39;, &#39;#NA&#39;, &#39;-1.#IND&#39;, &#39;-1.#QNAN&#39;,
                &#39;-NaN&#39;, &#39;-nan&#39;, &#39;1.#IND&#39;, &#39;1.#QNAN&#39;, &#39;&lt;NA&gt;&#39;, &#39;N/A&#39;, &#39;NA&#39;,
                &#39;NULL&#39;, &#39;NaN&#39;, &#39;n/a&#39;, &#39;nan&#39;, &#39;null&#39;]

df = pd.read_csv(&#39;filename.csv&#39;, 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(&#39;filename.csv&#39;, 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(&quot;NaN&quot;, &quot;None&quot;)
df = df.replace(&quot;None&quot;, &quot;None&quot;)
df = df.replace(np.nan, &quot;None&quot;)

# 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.

huangapple
  • 本文由 发表于 2023年4月11日 16:54:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定