pandas 在熊猫数据框中用 NaN 替换 None
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23743460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace None with NaN in pandas dataframe
提问by AdamNYC
I have table x
:
我有桌子x
:
website
0 http://www.google.com/
1 http://www.yahoo.com
2 None
I want to replace python None with pandas NaN. I tried:
我想用熊猫 NaN 替换 python None 。我试过:
x.replace(to_replace=None, value=np.nan)
But I got:
但我得到了:
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'
How should I go about it?
我该怎么办?
回答by Guillaume Jacquenot
You can use DataFrame.fillna
or Series.fillna
which will replace the Python object None
, not the string 'None'
.
您可以使用DataFrame.fillna
orSeries.fillna
来替换 Python 对象None
,而不是字符串'None'
。
import pandas as pd
For dataframe:
对于数据框:
df.fillna(value=pd.np.nan, inplace=True)
For column or series:
对于列或系列:
df.mycol.fillna(value=pd.np.nan, inplace=True)
回答by Nickolai
Here's another option:
这是另一种选择:
df.replace(to_replace=[None], value=np.nan, inplace=True)
回答by Max Izadi
The following line replaces None
with NaN
:
以下行替换None
为NaN
:
df['column'].replace('None', np.nan, inplace=True)
回答by Doubledown
If you use df.replace([None], np.nan, inplace=True), this changed all datetime objects with missing data to object dtypes. So now you may have broken queries unless you change them back to datetime which can be taxing depending on the size of your data.
如果您使用 df.replace([None], np.nan, inplace=True),这会将所有缺少数据的日期时间对象更改为对象 dtypes。因此,现在您可能已经破坏了查询,除非您将它们改回日期时间,这可能会根据您的数据大小而增加。
If you want to use this method, you can first identify the object dtype fields in your df and then replace the None:
如果要使用此方法,可以先识别 df 中的对象 dtype 字段,然后替换 None:
obj_columns = list(df.select_dtypes(include=['object']).columns.values)
df[obj_columns] = df[obj_columns].replace([None], np.nan)
回答by Ashish Sharma
DataFrame['Col_name'].replace("None", np.nan, inplace=True)