Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [SOLVED on SO] Downsizing non-representative data in DataFrame
#1
Hi, folks,
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 example
Output:
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 Tongue solution?!

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.
Quote
#2
If anyone is interested - with trepidation, I posted this question on SO (those in the know will understand my reluctance), and - got an answer, without being hassled for the whole half an hour and counting Dance
ichabod801 likes this post
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  datetime intervals - dataframe selection (via plotted data) karlito 0 12 2 hours ago
Last Post: karlito
  How to add data to the categorical index of dataframe as data arrives? AlekseyPython 1 205 Oct-16-2019, 06:26 AM
Last Post: AlekseyPython
  Inserting data from python list into a pandas dataframe mahmoud899 0 438 Mar-02-2019, 04:07 AM
Last Post: mahmoud899
  Pandas nested json data to dataframe FrankC 1 5,263 Aug-14-2018, 01:37 AM
Last Post: scidam
  Trying to import JSON data into Python/Pandas DataFrame then edit then write CSV Rhubear 0 1,170 Jul-23-2018, 09:50 PM
Last Post: Rhubear

Forum Jump:


Users browsing this thread: 1 Guest(s)