logo
down
shadow

Running a groupby on a pivot table with Pandas


Running a groupby on a pivot table with Pandas

By : user7452842
Date : November 21 2020, 04:03 PM
Hope that helps You'll want to groupby 'RowID'. Since it's a level on the MultiIndex you pass 'RowID' to the level keyword.
code :
In [5]: df.groupby(level='RowID').sum()
Out[5]: 
            3029903181  3029903182  3029903183  3029903184  ResponseCount
RowID                                                                    
3029903189        1539        1587        1560        1576           6262


Share : facebook icon twitter icon
Pandas: keeping dates in order when using groupby or pivot table

Pandas: keeping dates in order when using groupby or pivot table


By : Maciej Prus
Date : March 29 2020, 07:55 AM
I hope this helps you . If you don't want groupby to sort the values (its default behaviour), just pass in sort=False:
code :
>>> df.groupby('date', sort=False).sum()
            WeightedReturn
date                      
15/07/2015        0.008064
16/07/2015       -0.007677
21/07/2015       -0.000347
Groupby and Pivot Pandas table

Groupby and Pivot Pandas table


By : ani Sharma
Date : March 29 2020, 07:55 AM
wish help you to fix your issue If I understand you correctly,pivot_table might be closer to what you need:
code :
df = df.pivot_table(index=["YrMnth", "Letter"], columns="Period", values="Amount")
Period          1   3   4   5   6   7   8   9   10  11  12
YrMnth  Letter                                            
2014-12 B      NaN NaN NaN NaN   0 NaN NaN NaN NaN NaN NaN
        C      NaN NaN NaN NaN   4   6   1   2   3   7   5
        D       10  17  16 NaN  12  14  11   8   9  15  13
2015-01 B       20  25 NaN  26  23  18  19  24  21  22 NaN
        C      NaN NaN NaN NaN NaN NaN NaN NaN  27 NaN NaN
 df = pd.pivot_table(df, index=["YrMnth", "Letter"], columns="Period", values="Amount")


Period          1   3   4   5   6   7   8   9   10  11  12
YrMnth  Letter                                            
2014-12 B      NaN NaN NaN NaN   0 NaN NaN NaN NaN NaN NaN
        C      NaN NaN NaN NaN   4   6   1   2   3   7   5
        D       10  17  16 NaN  12  14  11   8   9  15  13
2015-01 B       20  25 NaN  26  23  18  19  24  21  22 NaN
        C      NaN NaN NaN NaN NaN NaN NaN NaN  27 NaN NaN
Complex Groupby or Pivot Table Calculation in Python Pandas

Complex Groupby or Pivot Table Calculation in Python Pandas


By : djomles
Date : March 29 2020, 07:55 AM
should help you out IIUC, .groupby() on level should work. Starting with your sample data:
code :
df.set_index(['UNIT', 'CA', 'DATE', 'SCP'], inplace=True)

<class 'pandas.core.frame.DataFrame'>
MultiIndex: 14 entries, (R001, A058, 2013-08-01 00:00:00, 01-00-00) to (R001, A058, 2013-08-01 00:00:00, 01-00-01)
Data columns (total 4 columns):
TIME       14 non-null object
LABEL      14 non-null object
VALUES1    14 non-null int64
VALUES2    14 non-null int64
dtypes: int64(2), object(2)

                                   TIME    LABEL  VALUES1  VALUES2
UNIT CA   DATE       SCP                                          
R001 A058 2013-08-01 01-00-00  01:00:00  REGULAR   340751   194975
                     01-00-00  05:00:00  REGULAR   340753   194975
                     01-00-00  09:00:00  REGULAR   341251   194984
                     01-00-00  09:39:56  REGULAR   341440   194994
                     01-00-00  13:00:00  REGULAR   341808   195061
                     01-00-00  17:00:00  REGULAR   342030   195295
                     01-00-00  21:00:00  REGULAR   342214   195659
                     01-00-01  01:00:00  REGULAR   245262   221709
                     01-00-01  05:00:00  REGULAR   245262   221709
                     01-00-01  09:00:00  REGULAR   245428   221742
                     01-00-01  09:39:56  REGULAR   245508   221754
                     01-00-01  13:00:00  REGULAR   245620   221856
                     01-00-01  17:00:00  REGULAR   245679   222178
                     01-00-01  21:00:00  REGULAR   245743   222604
df.groupby(level=['UNIT', 'CA', 'DATE', 'SCP'])['VALUES1', 'VALUES2'].apply(lambda x: x.max()-x.min())

                               VALUES1  VALUES2
UNIT CA   DATE       SCP                       
R001 A058 2013-08-01 01-00-00     1463      684
                     01-00-01      481      895
Pivot table based on groupby in Pandas

Pivot table based on groupby in Pandas


By : Mehboob Ali
Date : March 29 2020, 07:55 AM
around this issue I have a dataframe like this: , You may looking for crosstab
code :
pd.crosstab([df.customer_id,df.date],df.category).reset_index(level=1,drop=True)
Out[102]: 
category     computer  drinks  food  toys
customer_id                              
1                   0       1     0     1
1                   0       0     1     0
2                   0       0     1     1
3                   1       0     0     0
How can I perform a value dependent pivot table/Groupby in Pandas?

How can I perform a value dependent pivot table/Groupby in Pandas?


By : YoGodro
Date : March 29 2020, 07:55 AM
seems to work fine Use get_dummies for indicators with max and add new column with aggregating sum:
code :
#pandas 0.23+
df1 = pd.get_dummies(df.set_index('Tran ID')['Category'], dtype=bool).max(level=0)
#oldier pandas versions
#df1 = pd.get_dummies(df.set_index('Tran ID')['Category']).astype(bool).max(level=0)
s = df.groupby('Tran ID')['Quantity'].sum()

df2 = df1.assign(Quantity = s).reset_index()
print (df2)
  Tran ID      A      B      C      D  Quantity
0     001   True   True   True  False        10
1     002   True  False   True  False         6
2     003  False  False  False   True         6
Related Posts Related Posts :
  • Filtering from data
  • Where is the problem about selenium with python?
  • ansible custom filter fails when importing python library
  • How to assign the label of one column to the new one based on group maximum in pandas
  • What is the best approach for isolating a single area of similar colour?
  • Creating multiple clients for topics
  • Why is my 'for loop', despite iterating over all keys, only acting on the last one?
  • Can someone tell me what's wrong, when I run it the browsers says "This site can’t be reached"
  • Error in setting up mitmproxy on alpine 3.9
  • From traditional loop to list comprehension
  • Django celery unregistered task | relative imports
  • How to add elements in a multi dimensional array
  • Async await with sqs receive messages not working properly
  • What is definition of 'NAME' in Python grammar
  • Easy method to move rows from df to another with coditions?
  • Changing the size of only a single plot in matplotlib, without altering figure parameters
  • Fastest way to use Vision API on 10,000+ images with python
  • How to install nvidia apex on Google Colab
  • Random numbers Continuous in python
  • Fetching data after a certain time interval(10 sec) from a continuously increasing database like mysql using flask
  • Using VLOOKUP with merge in Python
  • Calculate geographical distance between 5 cities with all the possible combinations of each city
  • How to filter a pandas dataframe using multiple partial strings?
  • Pygame- make bullet shoot toward cursor direction
  • Create SEQUENCE based dictionary from list
  • How to fix broken link from Django MEDIA_ROOT?
  • How can I display the current time left in a timer in a label?
  • Compute number of occurance of each value and Sum another column in Pandas
  • How to separate the prefix in words that are 'di'?
  • Handling network errors from an external API across an application
  • Want a pandas Series of Trips Completed to count(Request) ratio for each hour as index for the given dataframe
  • Access dict keys and list elements by same index to loop over and assign values
  • Find rows from the same dataframe based on condition
  • Read only specific part first two lines from text file in python
  • Python How to convert string to dataframe?
  • How to fix this my error code program? I use Python 3.6
  • Is there a way of getting this string down to 3 words?
  • Large difference between overall F Score for a custom Spacy NER model and Individual Entity F Score
  • Drop rows where timestamps are older than subsequent row
  • Implement a bottle spin
  • Unable to convert widows epoch time to normal date time
  • Values from a XML file
  • PyAudio readframes not ending when wav file completes
  • Could not load the module
  • How to change datetime.datetime(2012, 1, 1, 0, 0) to 1/1/2012 in Python?
  • How to create ASN.1 Sequence without NamedType?
  • How to locate specific sequences of words in a sentence efficiently
  • How can I generate a multi-step process in Django without changing pages (w/out a new request)?
  • Why does this list comprehension only "sometimes" work?
  • send html report with row collapsed
  • How to define a type hint to a argument (the argument's value is a class, all expected value is a subclass of a certain
  • How do I send a styled pandas DataFrame by e-mail without losing the format?
  • How to view/average a groupby dataframe when the data is a string?
  • Django 2.2 staticfiles do not work in development
  • Flag to enable/disable numba JIT compilation?
  • Trying to split byte in a byte array into two nibbles
  • Error in Query - missing FROM-clause entry for table - SQL
  • Reading double c structures of Dll with Ctypes in Python
  • Autofill missing row in database based on missing time range
  • Get the max of a nested dictionary
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org