Here I have concatenated some 5-6 columns and got the array in "new column1".
Now I want to remove blank strings from this column and get the result as shown in filtered column as follows:
This is part of big dataframe 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]
I tried to use following codes but its not helping.
def remve(a):
while("" in a):
a.remove("")
return a
df1[' filtered column ']=df1[' new_columnn1 '].astype(str).apply(remve)
CodePudding user response:
If your column contains list, try:
df['Filtered Column'] = df['new_column1'].apply(lambda x: [i for i in x if i != ''])
Output:
>>> 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]