Remove elements of a list column based on condition
Question:
I’d like to remove elements of a list in a pandas dataframe column that does not contain an underscore. How can I do this?
w = pd.DataFrame({'Column A': ['Cell 1', 'Cell 2', 'Cell 3'], 'Column B': [['a_1', 'a'], ['b1', 'b2'], ['c_1','c_2', 'c3']]})
Of which I want my results to be:
Column A Column B
0 Cell 1 [a_1]
1 Cell 2 []
2 Cell 3 [c_1, c_2]
Answers:
You can explode the list and search matching values:
>>> w['Column B'].explode().loc[lambda x: x.str.contains('_')].tolist()
['a_1', 'c_1', 'c_2']
I’d like to remove elements of a list in a pandas dataframe column that does not contain an underscore. How can I do this?
w = pd.DataFrame({'Column A': ['Cell 1', 'Cell 2', 'Cell 3'], 'Column B': [['a_1', 'a'], ['b1', 'b2'], ['c_1','c_2', 'c3']]})
Of which I want my results to be:
Column A Column B
0 Cell 1 [a_1]
1 Cell 2 []
2 Cell 3 [c_1, c_2]
You can explode the list and search matching values:
>>> w['Column B'].explode().loc[lambda x: x.str.contains('_')].tolist()
['a_1', 'c_1', 'c_2']