简体   繁体   English

Python - 在包含 NaN 的列/列表上使用 if-else 进行列表理解

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

There is a df column that has strings (with extra spaces) and NaNs.有一个包含字符串(带有额外空格)和 NaN 的 df 列。
I want to remove the extra spaces from the strings and keep the NaNs where they are.我想从字符串中删除多余的空格并将 NaN 保留在它们所在的位置。
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.在列表理解中包含“else”正常工作。 Why is it not working in this case?为什么它在这种情况下不起作用?
  2. Is there another way to strip a column of extra spaces?还有另一种方法可以去除一列额外的空格吗?

The placement of the else is not correct, the whole if/else thing is before the for . else的位置不正确,整个 if/else 都在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']]

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 语句放在理解的末尾可以正常工作。 If you have both, it needs to go in front.如果两者都有,则需要 go 在前面。 Confusing?令人困惑? Yes, trips me up all the time.是的,总是让我绊倒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM