Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pd df explode

df.explode('A')
Comment

pd.explode

stacked = df.stack().explode().reset_index()
stacked["uid"] = stacked.groupby(["level_0", "level_1"]).cumcount()
output = stacked.pivot(["level_0", "uid"], "level_1", 0).reset_index(drop=True).rename_axis(None, axis=1)

>>> output

        TGR1 TGR2 TGR3
0          1    5    4
1          7    8    1
2          5    1    8
3          9    1    3
4          1    7    2
..       ...  ...  ...
69         4    8    2
70         5    4    2
71         5    1    4
72         2    6    1
73         1    8    7

[74 rows x 3 columns]
Comment

pandas explode

	Product			Cost	Tags
0	RAM	    		100		Add-on,Electronics
1	Laptop			5000	Units,Electronics
2	Rubber Ducky	50		Units,Decoration
# above into below
	Tags		Cost
	Add-on		100
	Electronics	5100
	Units		5050
	Decoration	50
# code -->
df['Tags'] = df['Tags'].str.split(',')
df = df.explode('Tags') 
data.groupby('Tags').sum()
Comment

PREVIOUS NEXT
Code Example
Python :: flatten dict with lists as entries 
Python :: python loop over list 
Python :: python sort list case insensitive 
Python :: how to check if a list is empty in python 
Python :: python discord bot embed 
Python :: python iterate through list 
Python :: how to convert uppercase to lowercase and vice versa in python 
Python :: Python NumPy column_stack Function Syntax 
Python :: put grid behind matplotlib 
Python :: use decorator in class python 
Python :: how to convert r to python 
Python :: 1024x768 
Python :: python random distribution 
Python :: speed typing test python 
Python :: remove timezone from column pandas 
Python :: get more than one decimal in python 
Python :: group normalization 
Python :: get current scene file name godot 
Python :: utils/decorators.py", line 11, in __get__ raise AttributeError("This method is available only on the class, not on instances.") AttributeError: This method is available only on the class, not on instances. 
Python :: configure your keyboards 
Python :: medium seaaborn mathplot diesign styles 
Python :: check stl file for errors in pyvista 
Python :: python coding for y, you will also display a “bar” of ‘X’ characters to represent the number. For example, the prime number 2 would be represented as “X 2”. 
Shell :: add-apt-repository command not found 
Shell :: remove all docker iamges commandl 
Shell :: how to remove mysql workbench in ubuntu 
Shell :: git save password global 
Shell :: remove google chrome linux 
Shell :: install tkinter in ubuntu 
Shell :: curl not found 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =