在 pandas 数据框中将NaN替换为None [英] Replace None with NaN in pandas dataframe
本文介绍了在 pandas 数据框中将NaN替换为None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有桌子 x
:
website
0 http://www.google.com/
1 http://www.yahoo.com
2 None
我想用pandas NaN代替python None。我试过了:
I want to replace python None with pandas NaN. I tried:
x.replace(to_replace=None, value=np.nan)
但是我得到了:
TypeError: 'regex' must be a string or a compiled regular expression or a list or dict of strings or regular expressions, you passed a 'bool'
我应该怎么做?
推荐答案
您可以使用 DataFrame.fillna
或 Series.fillna
,它将替换Python对象 None
,而不是字符串'None'
。
You can use DataFrame.fillna
or Series.fillna
which will replace the Python object None
, not the string 'None'
.
import pandas as pd
import numpy as np
对于数据框:
df = df.fillna(value=np.nan)
对于列或系列:
df.mycol.fillna(value=np.nan, inplace=True)
这篇关于在 pandas 数据框中将NaN替换为None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文