How can I replace values with 'none' in a dataframe using pandas

+2 votes

I'm learning pandas and I came across this problem.

[code]



df.replace('-', None)
[/code]
and this returns a pretty strange result
[code]
0
0   - // this isn't replaced
1   3
2   2
3   5
4   1
5  -5
6  -1
7  -1 // this is changed to `-1`...
8   9
[/code]
what exactly am I doing wrong?
Apr 2, 2018 in Python by ariaholic
• 7,340 points
7,701 views

2 answers to this question.

+2 votes
Best answer

Actually in later versions of pandas this will give a TypeError:

df.replace('-', None)
TypeError: If "to_replace" and "value" are both None then regex must be a mapping

You can do it by passing either a list or a dictionary:

In [11]: df.replace('-', df.replace(['-'], [None]) # or .replace('-', {0: None})
Out[11]:
      0
0  None
1     3
2     2
3     5
4     1
5    -5
6    -1
7  None
8     9

But I recommend using NaNs rather than None:

In [12]: df.replace('-', np.nan)
Out[12]:
     0
0  NaN
1    3
2    2
3    5
4    1
5   -5
6   -1
7  NaN
8    9
answered Aug 13, 2018 by bug_seeker
• 15,360 points

selected Oct 12, 2018 by Omkar
0 votes

I found the solution using replace with a dict the most simple and elegant solution:

df.replace({'-': None})

You can also have more replacements:

df.replace({'-': None, 'None': None})

And even for larger replacements, it is always obvious and clear what is replaced by what - which is way harder for long lists, in my opinion.

answered Oct 12, 2018 by findingbugs
• 4,750 points

Related Questions In Python

0 votes
1 answer

How to replace values with None in Pandas data frame in Python?

Actually in later versions of pandas this ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 56,940 points
2,097 views
0 votes
1 answer

How can I lookup hostname using the IP address with a timeout in Python?

Good question. I actually was stuck with ...READ MORE

answered Feb 6 in Python by Nymeria
• 3,520 points
87 views
0 votes
1 answer

How do I create a dataframe using a dictionary in pandas?

Hi @Hannah, You need to define your dictionary ...READ MORE

answered 1 day ago in Python by Eric
10 views
0 votes
1 answer

How can I define a multidimensional array in python using ctype?

Here's one quick-and-dirty method: >>> A = ((ctypes.c_float ...READ MORE

answered Oct 9, 2018 in Python by ariaholic
• 7,340 points
409 views
0 votes
1 answer

How can I rename multiple files in a certain directory using Python?

Use os.rename(src, dst) to rename or move a file ...READ MORE

answered Nov 23, 2018 in Python by ariaholic
• 7,340 points
240 views
0 votes
1 answer
0 votes
0 answers

how do sort the columns in a dataframe using the pandas?

can you give an example using dataframe? READ MORE

May 6 in Python by Waseem
• 4,470 points
25 views
0 votes
1 answer

how to exit a python script in an if statement

This works fine for me: while True: ...READ MORE

answered Sep 19, 2018 in Python by bug_seeker
• 15,360 points
4,581 views
0 votes
1 answer

Create an empty list in python with certain size

Try this instead: lst = [None] * 10 The ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 15,360 points
855 views