Q:

Split (explode) pandas DataFrame string entry to separate rows

belongs to collection: Python Pandas Programs

0

Rows in pandas are the different cell (column) values that are aligned horizontally and also provide uniformity. Each row can have the same or different value. Rows are generally marked with the index number but in pandas, we can also assign index names according to the needs. In pandas, we can create, read, update and delete a column or row value.

Here, we are going to use pandas.DataFrame.explode() method.

Syntax:

DataFrame.explode(
    column, 
    ignore_index=False
    )

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Let us understand with the help of an example:

# Importing Pandas package as pd
import pandas as pd

# Creating a dictionary
d = {'A':[['a','b','c','d'],2,3,4],'B':[1,2,3,4]}

# Creating DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print(df)

# Expanding a particular column
result = df.explode('A')

# Display Result
print("Modified DataFrame:\n",result)

Output:

Example: Split (explode) pandas DataFrame

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to select with complex criteria from pandas Da... >>
<< How to add a column to DataFrame with constant val...