Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Pandas column of lists, create a row for each list element

>>> df
                samples  subject  trial_num
0  [-0.07, -2.9, -2.44]        1          1
1   [-1.52, -0.35, 0.1]        1          2
2  [-0.17, 0.57, -0.65]        1          3
3  [-0.82, -1.06, 0.47]        2          1
4   [0.79, 1.35, -0.09]        2          2
5   [1.17, 1.14, -1.79]        2          3
>>>
>>> s = df.apply(lambda x: pd.Series(x['samples']),axis=1).stack().reset_index(level=1, drop=True)
>>> s.name = 'sample'
>>>
>>> df.drop('samples', axis=1).join(s)
   subject  trial_num  sample
0        1          1   -0.07
0        1          1   -2.90
0        1          1   -2.44
1        1          2   -1.52
1        1          2   -0.35
1        1          2    0.10
2        1          3   -0.17
2        1          3    0.57
2        1          3   -0.65
3        2          1   -0.82
3        2          1   -1.06
3        2          1    0.47
4        2          2    0.79
4        2          2    1.35
4        2          2   -0.09
5        2          3    1.17
5        2          3    1.14
5        2          3   -1.79
Comment

PREVIOUS NEXT
Code Example
Python :: python concat list multiple times 
Python :: # swap variables 
Python :: .format() 
Python :: #check if the element exists in the list.#check if the element exists in the list. 
Python :: plotly dcc.interval bar graph with time 
Python :: scipy z value to pvalue 
Python :: how to use ci variables in python robot 
Python :: pairplot legend position 
Python :: load shapefile fiona multiline intersection 
Python :: collecion.alt shopify python 
Python :: pop tkinter to the front of the screen 
Python :: how to make pictures whit python 
Python :: asterisk triangle print 
Python :: matplotlib insert small subplot into subplot 
Python :: Math Module acos() Function in python 
Python :: enumerate zip together 
Python :: Combining functions 
Python :: mavproxy arm 
Python :: docstring return list of tuple 
Python :: python typing optional argument 
Python :: Python NumPy broadcast_arrays() Function Syntax 
Python :: python antigravity 
Python :: how to convrete .npz file to txt file in python 
Python :: Python NumPy row_stack Function Example with 2d array 
Python :: python os.listdir attributes 
Python :: Python __le__ magic method 
Python :: count matching in two strings 
Python :: how to do something daily python 
Python :: Python matplotlib multiple bars 
Python :: Double all numbers using a map() Function 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =