Reputation: 125
I have a column from dataframe that looks something like this:
[[20164401, nan], [26817203, nan], [50584001, nan]]
[[65829601, nan], [07935501, nan]]
[[87959305, [08678501, 47896001, 73737327]]
How can I unlist all of the lists in the row and if it's possible to remove the 'nan'? The expected output is:
[20164401, 26817203, 50584001]
[65829601, 07935501]
[87959305, 08678501, 47896001, 73737327]
I tried with new_mass_whitelist2 = new_mass_whitelist1.applymap(lambda x: x[0] if isinstance(x, list) else x)
,but is not working as expected, because there are still unlisted lists.
Upvotes: 0
Views: 2513
Reputation: 597
I don't have your DF to try on, but as long as the values are int
and np.nan
something similar to this should work:
new_mass_whitelist2 = new_mass_whitelist1.apply(lambda x: [j for i in x for j in i if j!=np.nan])
If the data is all strings you will change the np.nan
for 'nan'
and add lower()
if you want it to be a litle flexible with NaN or nan,
new_mass_whitelist2 = new_mass_whitelist1.apply(lambda x: [j for i in x for j in i if j.lower()!='nan'])
Upvotes: 2