如何从 pandas 中不同列的数组中删除空白字符串?

How to remove blank strings from arrays in different columns in pandas?

在这里,我连接了一些 5-6 列,并在“新列 1”中获得了数组。

现在我想从此列中删除空白字符串并得到过滤列中显示的结果,如下所示:

这是大数据框 df 的一部分。

new_column1                                                           Filtered Column
['631-335-3414', '631-677-3675', '', '', 458665290.0]
['', '216-937-5320', '', '01714-737668', '', 497622620.0]
['973-654-1561', '973-662-8988', '01347-368222','08-5558-9019', 427885282.0]
['', '', '01912-771311', '01302-601380', '02-6044-4682', 443795912.0]
['973-544-2677', '973-986-4456', '01547-429341', '01290-367248',"]
['', '', '01865-582516', '1362620532', '08-6522-8931', 42799688.0]
['303-301-4946', '303-521-9860', '', '', '02-5226-9402', 415961606.0]
['', 9403023036.0, 01340713951.0]


Filtered Column
['631-335-3414', '631-677-3675', 458665290]
['216-937-5320', '01714-737668', 497622620]
['973-654-1561', '973-662-8988', '01347-368222', '08-5558-9019', 427885282]
[ '01912-771311', '01302-601380', '02-6044-4682', 443795912]
['973-544-2677', '973-986-4456', '01547-429341', '01290-367248']
['01865-582516', '1362620532', '08-6522-8931', 42799688]
['303-301-4946', '303-521-9860', '02-5226-9402', 415961606]
[9403023036, 01340713951]

我尝试使用以下代码,但没有用。

def remve(a):
    while("" in a):
        a.remove("")
        return a 
df1[' filtered column ']=df1[' new_columnn1 '].astype(str).apply(remve)

如果您的列包含列表,请尝试:

df['Filtered Column'] = df['new_column1'].apply(lambda x: [i for i in x if i != ''])

输出:

>>> df
                                         new_column1                                    Filtered Column
0      [631-335-3414, 631-677-3675, , , 458665290.0]          [631-335-3414, 631-677-3675, 458665290.0]
1    [, 216-937-5320, , 01714-737668, , 497622620.0]          [216-937-5320, 01714-737668, 497622620.0]
2  [973-654-1561, 973-662-8988, 01347-368222, 08-...  [973-654-1561, 973-662-8988, 01347-368222, 08-...
3  [, , 01912-771311, 01302-601380, 02-6044-4682,...  [01912-771311, 01302-601380, 02-6044-4682, 443...
4  [973-544-2677, 973-986-4456, 01547-429341, 012...  [973-544-2677, 973-986-4456, 01547-429341, 012...
5  [, , 01865-582516, 1362620532, 08-6522-8931, 4...  [01865-582516, 1362620532, 08-6522-8931, 42799...
6  [303-301-4946, 303-521-9860, , , 02-5226-9402,...  [303-301-4946, 303-521-9860, 02-5226-9402, 415...
7                     [, 9403023036.0, 1340713951.0]                       [9403023036.0, 1340713951.0]