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'
CodePudding user response:
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)