在pandas数据帧中用NAN替换空格 [英] Replaces spaces with NAN in pandas dataframe
问题描述
我有一个包含三列的pandas数据框:
I have a pandas dataframe with three columns:
Name Name2 DateTime
2016-06-10 05:22
2016-06-10 05:23
2016-06-10 14:25
Guest Guest1 2016-06-10 15:32
我必须用NAN替换空格。因此,行1,2,3和4的AccountName和AccountName2应为NAN。我尝试了以下声明:
I have to replace empty spaces with NAN. So AccountName and AccountName2 of rows 1,2,3 and 4 should be NAN. I tried the below statement:
df3['Name'] = df3['Name'].replace(r'[^\s+]',np.nan, regex=True)
但是因为我之后有空格名称中的Guest,所有5行都替换为NAN。
But since I have white spaces after "Guest " in Name, all 5 rows get replaced with NAN.
编辑:
这是我们的实际数据。
This is our actual data.
Name Name2 DateTime
\t\t-\r\n\t \t\t-\r\n\t 2016-06-10 05:22
\t\t-\r\n\t \t\t-\r\n\t 2016-06-10 05:23
\t\t-\r\n\t \t\t-\r\n\t 2016-06-10 14:25
\t\tGuest\r\n\t \t\tGuest1\r\n\t 2016-06-10 15:32
我用它来删除那些转义字符。
I used this to remove those escape characters.
df['Name'] = df['Name'].str.replace('\r','').str.replace('\t','').str.replace('\n','').str.replace('-','')
删除这些字符后,我不确定现在在那个地方插入了什么。
After removing those characters, I am not sure what gets inserted in that place now.
推荐答案
另一个找到<$ c $的解决方案c>长度数据,然后通过 布尔索引
替换长度 0
或 1 $ c的所有数据$ c>:
Another solution which found length
of data and then by boolean indexing
replacing all data with length 0
or 1
:
print (df.applymap(len))
Name Name2 DateTime
0 0 0 16
1 0 0 16
2 0 0 16
3 5 6 16
df[df.applymap(len) < 2] = np.nan
print (df)
Name Name2 DateTime
0 NaN NaN 2016-06-10 05:22
1 NaN NaN 2016-06-10 05:23
2 NaN NaN 2016-06-10 14:25
3 Guest Guest1 2016-06-10 15:32
这篇关于在pandas数据帧中用NAN替换空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!