Pandas transform dataframe using groupby when count of a string in a column is maximum

pythondumb

I have a dataframe as follows:

ID        IndentNo      Status     System_text
162       1000025418    Reject     Short Description Error
162       1000025418    Reject     Delivery date Error
162       1000025418    Accept     

As we can see that for a single ID we have two different Status viz Reject and Accept.

Objective: I want this dataframe to be transformed in a way that if count of Reject is more than count of Accept, then final df should read as :

ID        IndentNo      Status           System_text
162       1000025418    Reject     Short Description Error, Deivery Date Error

When I am trying the following:

df_f = df_final.groupby(['IndentId','IndentNo'])['status'].agg(max).reset_index(name='system_message')

I am getting the following df:

ID        IndentNo      System_text
162       1000025418    Reject     

What I am missing here?

Alexander

You can use value_counts to get the count of each Accept and Reject in the series.

If you have more rejects than accepts, join the System_text of all the rejects, and return this result as a single row dataframe. Otherwise, just return the original dataframe.

def transform_rejects(df):
    counts = df['Status'].value_counts().to_dict()
    if counts.get('Reject', 0) > counts.get('Accept', 0):
        desc = ', '.join(df.loc[df['Status'].eq('Reject'), 'System_text'].tolist())
        return pd.DataFrame({'Status': ['Reject'], 'System_text': [desc]})
    return df

df2 = df.groupby(['ID', 'IndentNo']).apply(transform_rejects)
df2.index = df2.index.droplevel(2)
>>> df2
                Status                                   System_text
ID  IndentNo                                                        
162 1000025418  Reject  Short Description Error, Delivery date Error

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Keeping 'key' column when using groupby with transform in pandas

From Dev

pandas groupby count string occurrence over column

From Dev

pandas groupby count string occurrence over column

From Dev

How to get a new dataframe with column count out of groupby function in pandas?

From Dev

Python Pandas: groupby 3 dataframes with string, numeric and NaN values to a new dataframe using a common column

From Dev

Using Pandas GroupBy and size()/count() to generate an aggregated DataFrame

From Dev

Pandas Percentage count on a DataFrame groupby

From Dev

Find count of unique column elements after using groupby with pandas

From Dev

Can pandas groupby transform a DataFrame into a Series?

From Dev

Transform pandas groupby / aggregate result to dataframe

From Dev

Transform a dataframe column using pattern

From Dev

pandas add column to groupby dataframe

From Dev

Pandas dataframe groupby remove column

From Dev

Pandas DataFrame GroupBy sum/count to new DataFrame

From Dev

Pandas TypeError when trying to count NaNs in subset of dataframe column

From Dev

Getting count of unique values in pandas Dataframe when there is a list object in a column

From Dev

transform unique values using pandas groupby

From Dev

how to concat sets when using groupby in pandas dataframe?

From Dev

StopIteration error when using groupby method of Pandas DataFrame

From Dev

Restore hierarchical column index when using groupby in pandas

From Dev

pandas dataframe: subset by column + groupby another column

From Dev

Pandas dataframe - transform column values into individual columns

From Dev

Transform nested dictionary in Pandas DataFrame to column representation

From Dev

Pandas create new column with count from groupby

From Dev

Adding a 'count' column to the result of a groupby in pandas?

From Dev

How to reference groupby index when using apply, transform, agg - Python Pandas?

From Dev

Groupby df column using pandas

From Dev

how to count how many times a string occurs in a column using pandas

From Dev

Pandas, groupby and finding maximum in groups, returning value and count

Related Related

  1. 1

    Keeping 'key' column when using groupby with transform in pandas

  2. 2

    pandas groupby count string occurrence over column

  3. 3

    pandas groupby count string occurrence over column

  4. 4

    How to get a new dataframe with column count out of groupby function in pandas?

  5. 5

    Python Pandas: groupby 3 dataframes with string, numeric and NaN values to a new dataframe using a common column

  6. 6

    Using Pandas GroupBy and size()/count() to generate an aggregated DataFrame

  7. 7

    Pandas Percentage count on a DataFrame groupby

  8. 8

    Find count of unique column elements after using groupby with pandas

  9. 9

    Can pandas groupby transform a DataFrame into a Series?

  10. 10

    Transform pandas groupby / aggregate result to dataframe

  11. 11

    Transform a dataframe column using pattern

  12. 12

    pandas add column to groupby dataframe

  13. 13

    Pandas dataframe groupby remove column

  14. 14

    Pandas DataFrame GroupBy sum/count to new DataFrame

  15. 15

    Pandas TypeError when trying to count NaNs in subset of dataframe column

  16. 16

    Getting count of unique values in pandas Dataframe when there is a list object in a column

  17. 17

    transform unique values using pandas groupby

  18. 18

    how to concat sets when using groupby in pandas dataframe?

  19. 19

    StopIteration error when using groupby method of Pandas DataFrame

  20. 20

    Restore hierarchical column index when using groupby in pandas

  21. 21

    pandas dataframe: subset by column + groupby another column

  22. 22

    Pandas dataframe - transform column values into individual columns

  23. 23

    Transform nested dictionary in Pandas DataFrame to column representation

  24. 24

    Pandas create new column with count from groupby

  25. 25

    Adding a 'count' column to the result of a groupby in pandas?

  26. 26

    How to reference groupby index when using apply, transform, agg - Python Pandas?

  27. 27

    Groupby df column using pandas

  28. 28

    how to count how many times a string occurs in a column using pandas

  29. 29

    Pandas, groupby and finding maximum in groups, returning value and count

HotTag

Archive