Questions tagged [pandas-explode]

Questions relating to the explode method of pandas DataFrames or Series. Be sure to also include the base tag [pandas].

The explode method was added in pandas 0.25.0 (July 18, 2019).

explode transforms columns of list-like values so that each element is on its own row. By default, index values will be duplicated for these rows. Moreover, for the case of DataFrame.explode, the values in the columns that are not to be exploded will also be duplicated.

Useful Canonicals:

The Documentation:

60 questions
214
votes
16 answers

How to unnest (explode) a column in a pandas DataFrame, into multiple rows

I have the following DataFrame where one of the columns is an object (list type cell): df = pd.DataFrame({'A': [1, 2], 'B': [[1, 2], [1, 2]]}) Output: A B 0 1 [1, 2] 1 2 [1, 2] My expected output is: A B 0 1 1 1 1 2 3 2 1 4 …
BENY
  • 317,841
  • 20
  • 164
  • 234
77
votes
7 answers

Efficient way to unnest (explode) multiple list columns in a pandas DataFrame

I am reading multiple JSON objects into one DataFrame. The problem is that some of the columns are lists. Also, the data is very big and because of that I cannot use the available solutions on the internet. They are very slow and memory-inefficient…
Moh
  • 1,887
  • 3
  • 18
  • 29
38
votes
2 answers

How to implode(reverse of pandas explode) based on a column

I have a dataframe df like below NETWORK config_id APPLICABLE_DAYS Case Delivery 0 Grocery 5399 SUN 10 1 1 Grocery 5399 MON 20 2 2 Grocery…
krishna koti
  • 659
  • 1
  • 6
  • 10
9
votes
2 answers

Pandas Explode on Multiple columns

Using Pandas 0.25.3, trying to explode a couple of columns. Data looks like: d1 = {'user':['user1','user2','user3','user4'], 'paid':['Y','Y','N','N'] 'last_active':['11 Jul 2019','23 Sep 2018','08 Dec 2019','03 Mar 2018'], …
nos codemos
  • 569
  • 1
  • 5
  • 19
8
votes
4 answers

Fill in same amount of characters where other column is NaN

I have the following dummy dataframe: df = pd.DataFrame({'Col1':['a,b,c,d', 'e,f,g,h', 'i,j,k,l,m'], 'Col2':['aa~bb~cc~dd', np.NaN, 'ii~jj~kk~ll~mm']}) Col1 Col2 0 a,b,c,d aa~bb~cc~dd 1 e,f,g,h …
Erfan
  • 40,971
  • 8
  • 66
  • 78
7
votes
1 answer

pandas explode fails with KeyError: 0

How does a pandas.DataFrame.explode work? In the documentation: https://pandas.pydata.org/pandas-docs/version/0.25/reference/api/pandas.DataFrame.explode.html df = pd.DataFrame({'A': [[1, 2, 3], 'foo', [], [3, 4]], 'B':…
Georg Heiler
  • 16,916
  • 36
  • 162
  • 292
3
votes
4 answers

Expand embedded list of dictionnaries in a DataFrame as new columns of the DataFrame

I have a Pandas DataFrame looking like: import pandas as pd print(pd.__version__) df0 = pd.DataFrame([ [12, None, [{'dst': '925', 'object': 'Lok. Certification', 'admin': 'valid'}, {'dst': '935', 'object': 'Lok. Administration',…
swiss_knight
  • 5,787
  • 8
  • 50
  • 92
2
votes
3 answers

Python - How to extract values ​from different list of dictionaries in the rows of the dataframe

I have data frame as like df: id features 100 [{'city': 'Rio'}, {'destination': '2'}] 110 [{'city': 'Sao Paulo'}] 135 [{'city': 'Recife'}, {'destination': '45'}] 145 …
Cindy
  • 568
  • 7
  • 20
1
vote
1 answer

pandas pre-filter an exploded list

I have a list column in a pandas dataframe that needs to be exploded and filtered based on another list. df_have = pd.DataFrame({'user': ['emp_1', 'emp_2', 'emp_3', 'admin'], 'group_ids': [[5, 3], [4, 2, 3], [1, 4], [1, 2, 3,…
Grizzly2501
  • 113
  • 1
  • 3
  • 10
1
vote
1 answer

Convert specific list of strings cells into multiple rows and keep the other columns

I have pandas dataframe that looks like this: label pred gt label1 val1 val11 label2 ['str1', str2'] ['str1', 'str3', 'str4'] label3 foo box And I want to convert label2 row where I have lists of strings or None value to multiple…
Yana
  • 785
  • 8
  • 23
1
vote
1 answer

How to explode pandas dataframe columns and create new column with the key

I have pandas daraframe where I have multiple column which has list of dictionary. Ex: col1 col2 combine_col1 combine_col2 val1 val2 [{'x1':'v1','x2':'v2'}] [{'x3':'v3','x4':'v4','x5':'v5'}] val11 val22 …
Dcook
  • 899
  • 7
  • 32
1
vote
1 answer

dataframe: parse a column containing list of dicts: Traceback ValueError: cannot reindex on an axis with duplicate labels

I have one column(called 'data') in a dataframe which looks like this, each row has a list of dicts, starting with 2022-01-04, ended today, for example the 1st row is {'value': 18.76, 'date': '2022-01-04'}, {'value': 18.59, 'date': '2022-01-05'},…
neutralname
  • 383
  • 2
  • 4
  • 11
1
vote
1 answer

How to drop the last row of an exploded dataframe

I have a large dataframe, but to make this easy it looks something like this one below A B C 0 [a, b, c] 1 22 1 [d, e] 2 45 2 [f, g] 3 32 3 [h, i] 4 64 4 [j, k, l, m] 5 76 Now I have…
1
vote
3 answers

Pandas explode/split lists and reasign values

I have this code df = pd.DataFrame({'an':{0: 'f', 1: 'i', 2:'-' , 3:'-' , 4:'f' , 5:'c,f,i,j' , 6:'c,d,e,j'}, 'nv':{0: [-1], 1: [-1], 2: ['-'], 3:['-'] , 4:[-2] , 5:[-2,-1,-3,-1] , 6:[-2,-1,-2,-1]}, }) which…
josepmaria
  • 373
  • 1
  • 11
1
vote
1 answer

Call pandas explode on one column, and divide the other columns accordingly

I have a dataframe like the one below d = {"to_explode": [[1, 2, 3], [4, 5], [6, 7, 8, 9]], "numbers": [3, 2, 4]} df = pd.DataFrame(data=d) to_explode numbers 0 [1, 2, 3] 3 1 [4, 5] 4 2 [6, 7, 8, 9] 12 I want to…
travelsandbooks
  • 111
  • 1
  • 12
1
2 3 4