tajihiro
tajihiro

Reputation: 2443

How to replace values in all columns in Dataframe

I know how to replace values in specific columns value. Following example is how to replace value '[NULL]' to blank in 'col01'.

df['col01'] = df['col01'].str.replace('[NULL]', '')

However I have no idea to replace value without column names. I would like to make all columns as a replacement targets.

How can I make a code? Thanks.

Upvotes: 1

Views: 9334

Answers (1)

jezrael
jezrael

Reputation: 862671

I believe you need DataFrame.replace:

df = df.replace('[NULL]', '')

If need replace substrings:

df = df.replace('\[NULL\]', '', regex=True)

Sample:

df = pd.DataFrame({'a':['[NULL]','f','[NULL] aa'],
                   'b':['w','[NULL]','d [NULL]']})

print(df)
           a         b
0     [NULL]         w
1          f    [NULL]
2  [NULL] aa  d [NULL]

df1 = df.replace('[NULL]', '')
print (df1)
           a         b
0                    w
1          f          
2  [NULL] aa  d [NULL]

df2 = df.replace('\[NULL\]', '', regex=True)
print (df2)
     a   b
0        w
1    f    
2   aa  d 

Upvotes: 8

Related Questions