Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 648
Chris Dixon Avatar asked Dec 10 '25 18:12

Chris Dixon


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().
like image 79
wwnde Avatar answered Dec 12 '25 06:12

wwnde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!