Adding row to pandas dataframe where column contains additional value that should be another row?

Viewed 535

Simplified df where some rows contain additional entries in another flavor that should be another row:

   values more values   flavor another flavor
0       6         foo  caramel      chocolate
1       4         baz  vanilla            NaN

df = pd.DataFrame({"values": [6, 4],"more values": ["foo",  "baz"],"flavor": ["caramel", "vanilla"],"another flavor": ["chocolate",  np.nan],})

We need to add another row containing values from other columns, populating flavor with values from another flavor. We'd then drop another flavor to get desired_df:

   values more values     flavor
0       6         foo    caramel
1       6         foo  chocolate
2       4         baz    vanilla

desired_df = pd.DataFrame({"values": [6, 6, 4],"more values": ["foo", "foo", "baz"],"flavor": ["caramel", "chocolate",  "vanilla"],})

What's a practical way to do this? Is there an expression for this I could search for as a keyword?

1 Answers

Please use the loc accessor to slice suitable columns and then append. You can sort values if needed.

df.loc[:,:'flavor'].append(df.loc[:, df.columns != 'flavor'].rename(columns={'another flavor':'flavor'}),ignore_index=True).dropna().sort_index().
Related