熊猫数据框删除新的换行

时间:2020-06-13 05:05:24

标签: python python-3.x pandas

我有以下数据:

          all_data
0     \n\n\n\n\n\n\n\n
1      \n\n\n\n\n\n\n\n\n\n\n\n

如何删除'\n'字符?

我尝试了以下操作:

df.replace(r'\\n','',regex=True)
df = df.replace(r'\\n','',regex=True)

我在做什么错了?

2 个答案:

答案 0 :(得分:2)

执行此操作:

In [3067]: df['all_data'].str.replace(r'\\n','',regex=True) 
Out[3067]: 
0    
1    
Name: all_data, dtype: object

OR

In [3068]: df.apply(lambda x: x.str.replace(r'\\n',''))
Out[3068]: 
  all_data
0         
1       

答案 1 :(得分:2)

使用1个或多个正则表达式。

df.replace(r'\\n+', '', regex=True)

输出:

  all_data
0         
1         
相关问题