熊猫从字符串中删除字符

时间:2020-04-26 11:57:48

标签: python pandas

我想简化我的3行代码,以消除方括号成一行:

df = pd.DataFrame(dict(words=['[hello]',
                            '[hello,[Name, World, Max]',
                            np.nan, 
                            '[Goodbye]',
                            np.nan,
                            '[hello, [goodbye], hello]']))
df['words'].fillna("N/A", inplace=True)
df['words']=df['words'].str.extract("\[", "")
df['words']=df['words'].str.replace("\]", "")

我尝试了这里提到的几种方法,但是没有用(替换,扩展):remove characters from pandas column

2 个答案:

答案 0 :(得分:2)

我认为您需要使用[]中的值的Series.str.replace

df['words'] = df['words'].fillna("N/A").str.replace("[\]\[]", "")

或对正则表达式|使用or

df['words'] = df['words'].fillna("N/A").str.replace("\]|\[", "")

print (df)
                    words
0                   hello
1  hello,Name, World, Max
2                     N/A
3                 Goodbye
4                     N/A
5   hello, goodbye, hello

答案 1 :(得分:0)

import numpy as np

df = pd.DataFrame(dict(words=['[hello]',
                            '[hello,[Name, World, Max]',
                            np.nan, 
                            '[Goodbye]',
                            np.nan,
                            '[hello, [goodbye], hello]']))
df ['words'] = df['words'].fillna("N/A", inplace=False).replace('\[{0,1}\]{0,1}', '', regex=True)