How to split the following data into separate rows using Pandas?

Question:

I’m trying to get the values in the ‘Output’ column split into separate rows:

Category Output
Car Doc, writeback
Car Trace, dataview

This needs to be:

Category Output
Car Doc
Car Trace
Car writeback
Car dataview

I’ve tried:
new_df = auto_test_file.DataFrame(auto_test_file.Output.str.split('|').tolist(), index=auto_test_file.Region).stack()

but this gives the error:

AttributeError: ‘DataFrame’ object has no attribute ‘DataFrame’

Asked By: lostcoder

||

Answers:

import pandas as pd
d = {'Category': ["Car", "Car"], 'Output': ["Doc, writeback", "Trace, dataview"]}
df = pd.DataFrame(data=d)
df_splitted = pd.DataFrame(columns = df.columns)
for i in range(len(df)):
    outputs = df['Output'][i].split(',')
    for j in outputs:
        df_splitted.loc[len(df_splitted)] = [df['Category'][i], j]
print(df_splitted)
Answered By: Polatkan Polat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.