Python code to split cell into multiple rows in pandas dataframe
Question:
Answers:
The question is not very clear what cells / kind of cells you want to split, but anyway the way to do so is :
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'col1': ['A,B,C', 'D,E', 'F']})
# Use the str.split method to split the values in the 'col1' column
df = df['col1'].str.split(',', expand=True).stack().reset_index(level=1, drop=True).rename('col1')
# The resulting DataFrame will have multiple rows for each original row
print(df)
This code will result something like this output:
0 A
0 B
0 C
1 D
1 E
2 F
Try to get inspiration from this snippet !
The question is not very clear what cells / kind of cells you want to split, but anyway the way to do so is :
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'col1': ['A,B,C', 'D,E', 'F']})
# Use the str.split method to split the values in the 'col1' column
df = df['col1'].str.split(',', expand=True).stack().reset_index(level=1, drop=True).rename('col1')
# The resulting DataFrame will have multiple rows for each original row
print(df)
This code will result something like this output:
0 A
0 B
0 C
1 D
1 E
2 F
Try to get inspiration from this snippet !