logo
down
shadow

Filter after Groupby and Sum in pandas?


Filter after Groupby and Sum in pandas?

By : Marc-André Bumann
Date : October 25 2020, 09:10 AM
it fixes the issue It's as simple as passing the condition that you wish to evaluate, if I understand the question correctly.
code :
df = df[df["paramount"]["sum"] > 0]


Share : facebook icon twitter icon
Pandas groupby and filter

Pandas groupby and filter


By : Sylvain
Date : March 29 2020, 07:55 AM
I wish this helpful for you I think groupby is not necessary, use boolean indexing only if need all rows where V is 0:
code :
print (df[df.V == 0])
    C  ID  V  YEAR
0   0   1  0  2011
3  33   2  0  2013
5  55   3  0  2014
print(df.groupby(['ID']).filter(lambda x: (x['V'] == 0).any())) 
    C  ID  V  YEAR
0   0   1  0  2011
1  11   1  1  2012
2  22   2  1  2012
3  33   2  0  2013
4  44   3  1  2013
5  55   3  0  2014
print(df.groupby(['YEAR']).filter(lambda x: (x['V'] == 0).any())) 
    C  ID  V  YEAR
0   0   1  0  2011
3  33   2  0  2013
4  44   3  1  2013
5  55   3  0  2014
print(df[(df['V'] == 0).groupby(df['YEAR']).transform('any')]) 
   ID  YEAR  V   C
0   1  2011  0   0
3   2  2013  0  33
4   3  2013  1  44
5   3  2014  0  55
print((df['V'] == 0).groupby(df['YEAR']).transform('any')) 
0     True
1    False
2    False
3     True
4     True
5     True
Name: V, dtype: bool
groupby and filter pandas

groupby and filter pandas


By : Schalk de Wet
Date : March 29 2020, 07:55 AM
will be helpful for those in need Here's a solution using groupby + set. This should be extendable and does not require ordering:
code :
s = df.groupby('make')['sale'].apply(set)
res = df[df['make'].map(s) >= {0, 1}]

print(res)

      make   country other_columns  sale
0    honda     tokyo          data     1
1    honda  hirosima          data     0
2   toyota     tokyo          data     1
3   toyota  hirosima          data     0
6  ferrari     tokyo          data     1
7  ferrari  hirosima          data     0
8   nissan     tokyo          data     1
9   nissan  hirosima          data     0
pandas filtering after a groupby with groupby-specifc filter conditions?

pandas filtering after a groupby with groupby-specifc filter conditions?


By : Aleksandr Saveljev
Date : March 29 2020, 07:55 AM
help you fix your problem "oh, you are so close, just add in this little extra part!"
See below for little extra part.
code :
df = pd.DataFrame({'person':['Sue', 'Sue', 'Sue', 'Bill', 'Alfonso'],
               'date': ['2019-01-01','2019-01-02', '2019-01-03','2019-02-01', '2019-03-01'],
               'my_value': [5,10,20,10,5],
               'my_other_value': [3,2,9,6,8]})
df = df.sort_values(['person', 'date']).reset_index(drop=True)

>>> df

    person  date        my_value    my_other_value
0   Alfonso 2019-03-01  5           8
1   Bill    2019-02-01  10          6
2   Sue     2019-01-01  5           3
3   Sue     2019-01-02  10          2
4   Sue     2019-01-03  20          9
df2 = df.query('my_value == 10').groupby('person').first()['date'].reset_index()
df2 = df2.rename(columns={'date': 'first_date'})
>>> df2

    person  first_date
0   Bill    2019-02-01
1   Sue     2019-01-02
df_merged = pd.merge(df, df2, how='left', on=['person'])
>>> df_merged

    person  date        my_value    my_other_value  first_date
0   Alfonso 2019-03-01  5           8               NaN
1   Bill    2019-02-01  10          6               2019-02-01
2   Sue     2019-01-01  5           3               2019-01-02
3   Sue     2019-01-02  10          2               2019-01-02
4   Sue     2019-01-03  20          9               2019-01-02
grouped = df_merged[df_merged['date'] >= df_merged['first_date']].groupby('person')
>>> grouped['my_other_value'].mean()

person
Bill    6.0
Sue     5.5
Name: my_other_value, dtype: float64
How to filter pandas groupby object based on groupby.groups.keys()

How to filter pandas groupby object based on groupby.groups.keys()


By : Kayden Gold
Date : March 29 2020, 07:55 AM
will help you Setting the indices as Pop and Home generates the value 'pairs' and using isin() applies the filter needed:
code :
df1.set_index(['Pop', 'Homes'], inplace=True)
df2.set_index(['Pop', 'Homes'], inplace=True)

df1 = df1[df2.index.isin(df1.index)]

df1.reset_index(inplace=True)
print(df1)
How can I filter a Pandas GroupBy object and obtain a GroupBy object back?

How can I filter a Pandas GroupBy object and obtain a GroupBy object back?


By : Rafael Gomes
Date : March 29 2020, 07:55 AM
I hope this helps . If you want to combine a filter and an aggregate, the best way I can think of would be to combine your filter and aggregate using a ternary if inside apply, returning None for filtered groups, and then dropna to remove these rows from your final result:
Related Posts Related Posts :
  • Cannot install pyqt5 for python3.4 on windows 10
  • Gravity Problems
  • Where to position `import` modules inside an class?
  • Python OpenCV: Cannot resize image
  • Print on the same spot in IPython console
  • Disable logging except in tests
  • Writing json to file in s3 bucket
  • Sorting numpy array created by laspy
  • Open an XML file through URL and save it
  • How to build a 2-level dictionary?
  • error installing scipy using pip on windows 10
  • __str__ from my own matrix, python
  • python re how to Extract fields use findall()?
  • how to read a value from text HI file using python?
  • How to use horizontal scrolling in treeview,here i use tree view to make a table
  • Dependant widgets in tkinter
  • Read and write in a JSON file using python 2.x
  • How to fix the function issue while allowing it to be dynamic?
  • Set long strings as default value in class
  • What is the REGEX for any number with a string(letters and punctuations)?
  • pip with several version of python on windows
  • Submitting login form with scrapy
  • How do i edit the favicon in the Browsable API in Django REST framework?
  • multiprocessing.Pool.map_async doesn't seem to... do anything at all?
  • Python Selenium: Stale Element Reference Exception Error
  • Datetime conversion - How to extract the inferred format?
  • Import YAML variables automatically?
  • How to create a powershell shortcut for my python file
  • Python's 'set' operator doesn't work with numpy.nan
  • Pass object fields and one2many fields on same method - Odoo v8
  • Select columns based on column name and location in Pandas
  • Standardizing timeseries in Pandas using interpolation
  • How many tweets can be collected?
  • how format specifier taking value while tuple list is passed
  • How to print a numpy array with data type?
  • Timeout child thread for python3
  • How can I regroup a dataframe and accumulate a colume's values?
  • Bulk Insert into SQL Server with Python not working
  • Removing last rows of each group based on condition in a pandas dataframe
  • Why the css file can not be found in Django template?
  • targeting center of mass - scipy / numpy
  • Foursquare - get tips from VENUE_ID
  • Unpack a dictionary to format
  • encoding special characters in python2
  • Replacing integers with NaN results in the entire column becoming float dtype
  • Python 3.6 - BeautifulSoup4, parse table AttributeError: ResultSet object has no attribute 'findAll'
  • Convert panda date list to python list of date strings
  • escape response from Scrapy to parse json
  • How to create a same dropdown menu for different labels?
  • Why are some python variables uppercase whereas others are lowercase?
  • Machine Learning, What are the common techniques for feature engineering and presenting the model?
  • Modify value of a Django form field during clean() and validate again
  • Heroku Django app can't start up -- 'No module named site'
  • Getting list of dates (excluding weekends)
  • Im trying to create the regular expression to include the text and not the href
  • Python file.readline(2) reads first 2 charectars
  • Groupby with handling empty bin in python
  • Modifying Gcode
  • calling a value in a dictionary within a dictionary (reading a json file)
  • Bouncing ball invalid syntax why is that?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org