Pandas Explode Multiple Columns

By using Pandas DataFrame explode() function you can transform or modify each element of a list-like to a row (single or multiple columns), replicating the index values. This function converts the list elements to a row while replacing the index values and returning the DataFrame exploded list.

In this article, I will explain how to explode single and multiple columns in pandas DataFrame by using explode() function. If the array-like is empty, a missing value NaN will be placed for that row.

1. Quick Examples of Explode Multiple Columns

If you are in a hurry, below are some quick examples of how to explode a single column and multiple columns in pandas DataFrame.


# Below are a quick example

# Example 1: use DataFrame.explode() function
df2 = df.explode('A')

# Example 2: Explode single column 
# using DataFrame.explode()function
df2 = df.explode(list('A'))

# Example 3: Explode single column & ignore_index
df2 = df.explode('A',ignore_index=True)

# Example 4: Explode multiple columns 
# Using DataFrame.explode() function
df2 = df.explode(list('AC'))

# Example 5: Use DataFrame.explode() Function 
# ignore_index
df2 = df.explode(list('AC'), ignore_index=True)

2. Syntax of DataFrame.explode()

Following is the syntax of DataFrame.explode() function.


# Syntax of DataFrame.explode()
DataFrame.explode(column, ignore_index=False)

2.1 Parameters of explode()

Following are the parameters of the pandas DataFrme explode() function.

  • column – IndexLabel: column to explode. For multiple columns, specify a non-empty list with each element being str or tuple.
  • ignore_index – boolean, default False. If True, the resulting index will be labeled 0, 1, …, n – 1.

2.2 Return value of explode()

It returns exploded lists to rows of the subset columns; the index will be duplicated for these rows.

Create DataFrame

Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are A, B, and C.


# Create pandas DataFrame
import pandas as pd
import numpy as np
technologies = (
    {'A': [["Spark","PySpark","Python"], 'Course', [], ["Java","pandas"]],
     'B': [25000,15000,30000,40000],
     'C': [['30days','40days','35days'], np.nan, [], ['40days','55days']]})
df = pd.DataFrame(technologies)
print(df)

Yields below output.

pandas explode

3. Explode Single Column Using DataFrame.explode()

You can use DataFrame.explode() function to convert each element of the specified single column "A" into a row (each value in a list becomes a row). This turns every element of the list A into a row. If the array-like is empty, the empty lists will be expanded into a NaN value.


# Use DataFrame.explode() function
df2 = df.explode('A')
print(df2)

# Explode single column using list()
df2 = df.explode(list('A'))
print(df2)

The above examples yield the below output.

pandas explode multiple columns

4. Explode Multiple Columns Using DataFrame.explode()

Alternatively, You can also use explode() function to explode multiple columns together using the explode function in pandas DataFrame. It returns DataFrame exploded lists to rows of the subset columns; the index will be duplicated for these rows. The following example explods multiple columns A and C.


# Explode multiple columns 
# Using DataFrame.explode() function
df2 = df.explode(list('AC'))
print(df2)

Yields below output.

pandas explode list to rows

5. Use DataFrame.explode() Function & ignore_index

You can pass ignore_index=True to DataFrame.explode() function to reset the index on DataFrame.


# Use DataFrame.explode() Function & ignore_index
df2 = df.explode(list('AC'), ignore_index=True)
print(df2)

Yields below output.


# Output
         A      B       C
0    Spark  25000  30days
1  PySpark  25000  40days
2   Python  25000  35days
3   Course  25000     NaN
4      NaN  25000     NaN
5     Java  25000  40days
6   pandas  25000  55days

6. Complete Example of Explode Multiple Columns


import pandas as pd
import numpy as np
technologies = ({'A': [["Spark","PySpark","Python"], 'Course', [], ["Java","pandas"]],
                   'B': 25000,
                   'C': [['30days','40days','35days'], np.nan, [], ['40days','55days']]})
df = pd.DataFrame(technologies)
print(df)

# use DataFrame.explode() function
df2 = df.explode('A')
print(df2)

# Explode single column 
# using DataFrame.explode()function
df2 = df.explode(list('A'))
print(df2)

# Explode single column & ignore_index
df2 = df.explode('A',ignore_index=True)
print(df2)

# Explode multiple columns 
# Using DataFrame.explode() function
df2 = df.explode(list('AC'))
print(df2)

# Use DataFrame.explode() Function & ignore_index
df2 = df.explode(list('AC'), ignore_index=True)
print(df2)

7. Conclusion

In this article, I have explained how to explode single and multiple columns (list to rows) of Pandas DataFrame by using DataFrame.explode() function with examples.

Happy Learning !!

Related Articles

References

Leave a Reply