logo
down
shadow

How to move pandas data from index to column after multiple groupby


How to move pandas data from index to column after multiple groupby

By : Noty Ferdous
Date : November 21 2020, 12:01 PM
wish of those help I have the following pandas dataframe: , Method #1: reset_index()
code :
>>> g
              uses  books
               sum    sum
token   year             
xanthos 1830     3      3
        1840     3      3
        1868     2      2
        1875     1      1

[4 rows x 2 columns]
>>> g = g.reset_index()
>>> g
     token  year  uses  books
                   sum    sum
0  xanthos  1830     3      3
1  xanthos  1840     3      3
2  xanthos  1868     2      2
3  xanthos  1875     1      1

[4 rows x 4 columns]
>>> g = dfalph[['token', 'year', 'uses', 'books']].groupby(['token', 'year'], as_index=False).sum()
>>> g
     token  year  uses  books
0  xanthos  1830     3      3
1  xanthos  1840     3      3
2  xanthos  1868     2      2
3  xanthos  1875     1      1

[4 rows x 4 columns]


Share : facebook icon twitter icon
How to groupby an index as well as a column in pandas

How to groupby an index as well as a column in pandas


By : user2821229
Date : March 29 2020, 07:55 AM
I hope this helps . Let's say I have a simple dataframe which has an index of a datetime and three columns - one being a value and one being an aggregating indicator and the last being a unique identifier. The index is not unique across the dataframe, as it is duplicated over multiple instances of the indicator. ie it looks like this: , You can do it like this:
code :
>>> df.reset_index().groupby(['index','aggregating indicator'])['val'].sum().reset_index()
   index aggregating indicator  val
0  1-Jan                 set_a    4
1  1-Jan                 set_b    5
2  2-Jan                 set_a    6
3  2-Jan                 set_b    6
How to apply get_group to groupby data in pandas when the groupby column is a category

How to apply get_group to groupby data in pandas when the groupby column is a category


By : Jonathan
Date : March 29 2020, 07:55 AM
will help you there must be a space between 0, and 10 - like '(0, 10]'.
Here is a small demonstration:
code :
df = pd.DataFrame({'age': np.random.randint(10,30,20)})
df['Age_group'] = pd.cut(df.age, bins=[10, 15, 20, 25, 30])
In [141]: df.groupby('Age_group').get_group('(10, 15]')
Out[141]:
    age Age_group
1    11  (10, 15]
6    12  (10, 15]
11   13  (10, 15]
12   14  (10, 15]
14   15  (10, 15]
15   12  (10, 15]
17   14  (10, 15]
18   13  (10, 15]
In [142]: df.groupby('Age_group').get_group('(10,15]')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-142-53b04eccd579> in <module>()
----> 1 df.groupby('Age_group').get_group('(10,15]')

...

KeyError: '(10,15]'
In [139]: df
Out[139]:
    age Age_group
0    25  (20, 25]
1    11  (10, 15]
2    27  (25, 30]
3    24  (20, 25]
4    27  (25, 30]
5    10       NaN
6    12  (10, 15]
7    20  (15, 20]
8    16  (15, 20]
9    29  (25, 30]
10   21  (20, 25]
11   13  (10, 15]
12   14  (10, 15]
13   21  (20, 25]
14   15  (10, 15]
15   12  (10, 15]
16   29  (25, 30]
17   14  (10, 15]
18   13  (10, 15]
19   19  (15, 20]
Pandas groupby: how to select adjacent column data after selecting a row based on data in another column in pandas group

Pandas groupby: how to select adjacent column data after selecting a row based on data in another column in pandas group


By : user2446312
Date : March 29 2020, 07:55 AM
wish of those help I have a database as partially shown below. For each date, there are entries for duration (1-20 per date), with items (100s) listed for each duration. Each item has several associated data points in adjacent columns, including an identifier. For each date, I want to select the largest duration. Then, I want to find the item with a value closest to a given input value. I would like to then obtain the ID for that item to be able to follow the value of this item through its time in the database. , You could do something like the following:
code :
target_value = 15
df['max_duration'] = df.groupby('Date')['Duration'].transform('max')
df.query('max_duration == Duration')\
  .assign(dist=lambda df: np.abs(df['Value'] - target_value))\
  .assign(min_dist=lambda df: df.groupby('Date')['dist'].transform('min'))\
  .query('min_dist == dist')\
  .loc[:, ['Date', 'ID']
        Date ID
4   1/1/2018  e
11  1/2/2018  e
Move groupby selected columns into a new pandas column as dictionary

Move groupby selected columns into a new pandas column as dictionary


By : Krogsa
Date : March 29 2020, 07:55 AM
I wish this help you I have the following pandas DataFrame in python: , Also you can use:
code :
df.groupby('id')['field1','field2'].apply(lambda x: x.to_dict('r')).rename('fields').reset_index()
   id                                             fields
0   1  [{'field1': 1, 'field2': 'a'}, {'field1': 2, '...
1   2  [{'field1': 3, 'field2': 'c'}, {'field1': 4, '...
2   3                     [{'field1': 5, 'field2': 'e'}]
Pandas: groupby column and set it as index

Pandas: groupby column and set it as index


By : timulacram
Date : March 29 2020, 07:55 AM
should help you out If I have a dataframe such as this: , You can set C as an index and then sort it :
code :
df1.set_index('C').sort_index(axis=0)
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