Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

pandas - 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?
question from:https://stackoverflow.com/questions/66055907/python-list-comprehension-with-if-else-on-column-list-containing-nans

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...