Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

after groupby how to add values in two rows to a list

In [1]: df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6]})
        df

Out[1]: 
   a  b
0  A  1
1  A  2
2  B  5
3  B  5
4  B  4
5  C  6

In [2]: df.groupby('a')['b'].apply(list)
Out[2]: 
a
A       [1, 2]
B    [5, 5, 4]
C          [6]
Name: b, dtype: object

In [3]: df1 = df.groupby('a')['b'].apply(list).reset_index(name='new')
        df1
Out[3]: 
   a        new
0  A     [1, 2]
1  B  [5, 5, 4]
2  C        [6]
Comment

PREVIOUS NEXT
Code Example
Python :: python file reading 
Python :: pandas distinct 
Python :: How to recursively sort the elements of a stack, in Python? 
Python :: install python 3.7 centos 
Python :: converting int to binary python 
Python :: pandas write to excel 
Python :: sort list by key 
Python :: tqdm progress bar python 
Python :: python tkinter fenstergröße 
Python :: setting p a virtual envioronment 
Python :: post to instagram from pc python 
Python :: get text selenium 
Python :: python list.peek 
Python :: chrome driver in python selenium not working 
Python :: -1 in numpy reshape 
Python :: object to int and float conversion pandas 
Python :: Simple Scatter Plot in matplotlib 
Python :: remove punctuation python string library 
Python :: train test split 
Python :: How to scale a pandas dataframe 
Python :: os.chdir python 
Python :: python get last element of list 
Python :: label point matplotlib 
Python :: max float python 
Python :: fill zero behind number python 
Python :: self-xss meaning 
Python :: python write line break 
Python :: hasattr in python 
Python :: play sound on python 
Python :: python parcourir un dictionnaire 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =