删除数据框系列的数组列中带有空格(“”)的空字符串

时间:2019-11-20 15:08:38

标签: python pandas dataframe list-comprehension

示例:

Column 1

[1, 3, " "]

[2, " ", 3]

etc.

有一个快速的列表理解功能,我可以只保留整数吗?

1 个答案:

答案 0 :(得分:0)

您可能正在寻找类似的东西:

a = [1, 3, " "]
b = [i for i in a if i != " "]

print(b) #> [1, 3]

如果要添加其他空格以删除:

a = [1, 3, " ", ""]
b = [i for i in a if i not in (" ", "")]

如果您只想添加int(如果有strfloat等,此方法将不通用):

a = [1, 3, " ", ""]
b = [i for i in a if isinstance(i, int)]