rahul-ahuja
rahul-ahuja

Reputation: 1424

Python - List comprehension with if-else on column / list containing NaNs

There is a df column that has strings (with extra spaces) and NaNs.
I want to remove the extra spaces from the strings and keep the NaNs where they are.
I used the following code but it is giving a syntax error:

a = pd.DataFrame({'col':[np.nan, np.nan,'Java', np.nan,'Java']})
a['col2'] = [i.strip() for i in a.loc[:,'col'] if isinstance(i, str) else i]
a
## The error I'm getting on using else
#> a['col2'] = [i.strip() for i in a.loc[:,'col'] if isinstance(i, str) else i]
#> SyntaxError: invalid syntax                                         ^
## Removing "else i" prevents the error, but then does not include the NaNs 
in the result which gives the following error:
#> ValueError: Length of values (2) does not match length of index (5)

Question

  1. Including 'else ' in list comprehension works normally. Why is it not working in this case?
  2. Is there another way to strip a column of extra spaces?

Upvotes: 0

Views: 661

Answers (2)

JarroVGIT
JarroVGIT

Reputation: 5324

The placement of the else is not correct, the whole if/else thing is before the for. Here is a working example:

a = pd.DataFrame({'col':[np.nan, np.nan,'Java', np.nan,'Java']})
a['col2'] = [i.strip() if isinstance(i, str) else i for i in a.loc[:,'col']]

Upvotes: 2

ScienceSnake
ScienceSnake

Reputation: 617

The order is wrong. Try

a['col2'] = [i.strip() if isinstance(i, str) else i for i in a.loc[:,'col']]

Placing an if statement at the end of a comprehension works fine if there is no else. If you have both, it needs to go in front. Confusing? Yes, trips me up all the time.

Upvotes: 1

Related Questions