Understand the idea
concat appends tables in the order listed. It matches column names, rather than looking for matching records.
A small example
Batches with indices [0, 1] and [0, 1] produce [0, 1, 0, 1], or [0, 1, 2, 3] with ignore_index=True.
Follow the code
Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.
pd.concat([first, second], ignore_index=True)What each part does
pd.concat([first, second])- stack tables in list order
ignore_index=True- number new rows from zero
Your inputs
The editable setup on the right creates df and the additional inputs shown below. Run executes the setup and your work from top to bottom.
| candy | flavour | price | rating | shelf |
|---|---|---|---|---|
| Gummy Bear | fruity | 1.2 | 4.1 | A |
| Choco Pop | chocolate | 2.1 | 4.6 | B |
| Mint Bite | mint | 1.5 | 3.8 | A |
| Berry Loop | fruity | 2.8 | 4.4 | B |
| Cocoa Cube | chocolate | 3.4 | 4.9 | A |
| Lemon Drop | fruity | 1.8 | 4 | B |
| candy | flavour | price | rating | shelf |
|---|---|---|---|---|
| Gummy Bear | fruity | 1.2 | 4.1 | A |
| Choco Pop | chocolate | 2.1 | 4.6 | B |
| Mint Bite | mint | 1.5 | 3.8 | A |
| candy | flavour | price | rating | shelf |
|---|---|---|---|---|
| Berry Loop | fruity | 2.8 | 4.4 | B |
| Cocoa Cube | chocolate | 3.4 | 4.9 | A |
| Lemon Drop | fruity | 1.8 | 4 | B |
Your task · Follow
- Stack the supplied first and second tables, in that order, with a fresh index.
- Use: concat().
Hint
concat takes a list of tables; ignore_index makes fresh row labels.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
pd.concat([first, second], ignore_index=True)