Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas groupby apply list

# import pandas library
import pandas as pd
  
# create a dataframe
df = pd.DataFrame({'a': ['A', 'A', 'B',
                          'B', 'B', 'C',
                          'C', 'D'], 
                    'b': [1, 2, 5,
                          3, 5, 4,
                          8, 6]})
                   
# convert values of each group
# into a list
groups = df.groupby('a')['b'].apply(list)
  
print(groups)
  
# groups store in a new 
# column called listvalues
df1 = groups.reset_index(name 
                         = 'listvalues')
# show the dataframe
df1
Comment

PREVIOUS NEXT
Code Example
Python :: input command in python shell 
Python :: jupyter markdown new line 
Python :: python pyqt5 sleep 
Python :: python requests response get text 
Python :: python extend list 
Python :: one-line for loop python 
Python :: list python virtual environments 
Python :: django content type 
Python :: django cookies 
Python :: python check if 3 values are equal 
Python :: sort list alphabetically python 
Python :: rotate image in pygame 
Python :: elon musk wikipedia 
Python :: multiply each element in list python 
Python :: palindrome string python 
Python :: python tips and tricks 
Python :: if else python 
Python :: replace string if it contains a substring pandas 
Python :: python read lines 
Python :: pytest multi thread 
Python :: how to start an exe file in python 
Python :: df to csv 
Python :: how to check if a input is an integer python 
Python :: how to make python turn a list into a text file grapper 
Python :: changing plot background color in python 
Python :: python get memory address 
Python :: timedelta 
Python :: sympy function definition 
Python :: std python 
Python :: python how to get user input 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =