Hi, folks,
I occasionally dabble in
I hacked around the problem by this inellegant solution
Anyone can suggest a more
solution?!
Thanks in advance
I occasionally dabble in
pandas
- but I cannot claim deep knowledge. Today I had to filter out some rows from a DataFrame
based on occurence of a value in a certain column. As in this exampleOutput:In [57]: table = pd.DataFrame([[2, 'a'], [3, 'b'], [2, 'c'], [4, 'd'], [4, 'e'], [5, 'f']],
...: columns=('group', 'letter'))
...: print(table)
...:
group letter
0 2 a
1 3 b
2 2 c
3 4 d
4 4 e
5 5 f
I want to remove all rows where a value in the group
column appears only once.I hacked around the problem by this inellegant solution
Output:In [58]: pd.concat(df for _, df in table.groupby(by=['group']) if len(df) > 1)
Out[58]:
group letter
0 2 a
2 2 c
3 4 d
4 4 e
But I bet there are proper ways to achieve the same goal.Anyone can suggest a more
pandaic

Thanks in advance
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.