want to remove ‘None’ from all rows of a Dataframe… here is a solution to the problem.
want to remove ‘None’ from all rows of a Dataframe
Hello, looks like people are confused, so I deleted my attempt, leaving only the highly relevant part of the question :
Code to create the data frame:
Sample = [{'account': 'Jones LLC', 'Jan': 150, 'Feb': 200, 'Mar': [.332, .326, .329]},
{'account': 'Alpha Co', 'Jan': 200, 'Feb': 210, 'Mar': [[.234, .246], None]},
{'account': 'Blue Inc', 'Jan': 50, 'Feb': 90, 'Mar': [[.23], [.745, .398, .923], None] }]
df = pd. DataFrame(Sample)
Data frame visualization:
df:
account Jan Feb Mar
Jones LLC | 150 | 200 | [.332, .326, .329]
Alpha Co | 200 | 210 | [[.234, .246], None]
Blue Inc | 50 | 90 | [[.23], [.745, .398, .923], None]
I need a function that can be applied to df to remove the “none” value from df[‘Mar’], the goal is to return this value:
df:
account Jan Feb Mar
Jones LLC | 150 | 200 | [.332, .326, .329]
Alpha Co | 200 | 210 | [.234, .246]
Blue Inc | 50 | 90 | [.23], [.745, .398, .923]
Solution
You can nest the list to understand:
df['Mar'] = [[x for x in inner_list if x is not None] for inner_list in df['Mar']]
You can also use filter to filter
out None
values
df['Mar'] = [list(filter(lambda x: x is None, inner_list)) for inner_list in df['Mar']]