I have a Pandas Dataframe as shown below:
我有一个熊猫数据帧,如下图所示:
1 2 3
0 a NaN read
1 b l unread
2 c NaN read
I want to remove the NaN values with an empty string so that it looks like so:
我想用空字符串删除NaN值,以便它看起来像这样:
1 2 3
0 a "" read
1 b l unread
2 c "" read
5 个解决方案
#1
82
import numpy as np
df1 = df.replace(np.nan, '', regex=True)
This might help. It will replace all NaNs with an empty string.
这可能帮助。它将替换所有的NaN为空字符串。
#2
150
Slightly shorter is:
稍短是:
df = df.fillna('')
This will fill na's (e.g. NaN's) with ''.
这将用''填充na(例如NaN)。
Edit: If you want to fill a single column, you can use:
编辑:如果要填充单个列,可以使用:
df.column1 = df.column1.fillna('')
#3
30
If you are reading the dataframe from a file (say CSV or Excel) then use :
-
df.read_csv(path , na_filter=False)
- df.read_csv(路径,na_filter =假)
df.read_excel(path , na_filter=False)
- df.read_excel(路径,na_filter =假)
This will automatically consider the empty fields as empty strings ' '
这会自动将空字段视为空字符串''
If you already have the Dataframe
df = df.replace(np.nan, '', regex=True)
- DF = df.replace(np.nan, '',正则表达式=真)
df = df.fillna('')
- df = df.fillna('')
#4
0
If you are converting Dataframe to Json: NaN will give error so best solution is in this use case is to replace NaN with None. Here is how: df1 = df.where((pd.notnull(df)), None)
如果要转换数据帧到JSON:NaN的将报错所以最好的办法是在这种使用情况下是无以取代的NaN。下面是如何:DF1 = df.where((pd.notnull(DF)),无)
#5
0
Use a formmatter. Plus, you don't modify your DataFrame:
使用formmatter。另外,您不要修改你的数据帧:
df = pd.DataFrame({
'A': ['a', 'b', 'c'],
'B': [np.nan, 1, np.nan],
'C': ['read', 'unread', 'read']})
print df.to_string(
formatters={'B': lambda x: '' if pd.isnull(x) else '{:.0f}'.format(x)})
To get:
要得到:
A B C
0 a read
1 b 1 unread
2 c read
#1
82
import numpy as np
df1 = df.replace(np.nan, '', regex=True)
This might help. It will replace all NaNs with an empty string.
这可能帮助。它将替换所有的NaN为空字符串。
#2
150
Slightly shorter is:
稍短是:
df = df.fillna('')
This will fill na's (e.g. NaN's) with ''.
这将用''填充na(例如NaN)。
Edit: If you want to fill a single column, you can use:
编辑:如果要填充单个列,可以使用:
df.column1 = df.column1.fillna('')
#3
30
If you are reading the dataframe from a file (say CSV or Excel) then use :
-
df.read_csv(path , na_filter=False)
- df.read_csv(路径,na_filter =假)
df.read_excel(path , na_filter=False)
- df.read_excel(路径,na_filter =假)
This will automatically consider the empty fields as empty strings ' '
这会自动将空字段视为空字符串''
If you already have the Dataframe
df = df.replace(np.nan, '', regex=True)
- DF = df.replace(np.nan, '',正则表达式=真)
df = df.fillna('')
- df = df.fillna('')
#4
0
If you are converting Dataframe to Json: NaN will give error so best solution is in this use case is to replace NaN with None. Here is how: df1 = df.where((pd.notnull(df)), None)
如果要转换数据帧到JSON:NaN的将报错所以最好的办法是在这种使用情况下是无以取代的NaN。下面是如何:DF1 = df.where((pd.notnull(DF)),无)
#5
0
Use a formmatter. Plus, you don't modify your DataFrame:
使用formmatter。另外,您不要修改你的数据帧:
df = pd.DataFrame({
'A': ['a', 'b', 'c'],
'B': [np.nan, 1, np.nan],
'C': ['read', 'unread', 'read']})
print df.to_string(
formatters={'B': lambda x: '' if pd.isnull(x) else '{:.0f}'.format(x)})
To get:
要得到:
A B C
0 a read
1 b 1 unread
2 c read