Think of pandas.concat as stacking compatible data tables into one larger table. It is a top-level pandas function that accepts a list of pandas objects, concatenates them along an axis, and returns a new combined object. section 2.10 section 3.6
Add products to a catalog
Create new_products with noncolliding indexes, then combine it with the existing catalog using df = pd.concat([df, new_products]). Because the new rows omit sales, pandas preserves the column and fills those entries with NaN. Select them with df.loc[[5, 6, 7], 'sales'] and assign the missing values. section 2.10
Use concatenation when separate tables represent compatible records that should be analyzed together:
- Load July 2019 and July 2020 taxi data, add a year column to each frame, and concatenate them before comparing ride counts, revenue, or payment behavior. section 3.6
- Load weather files for Chicago, Los Angeles, and Boston, label each frame with its city, and concatenate them before grouping and plotting. section 11.6
- Experiment by adding one product without pd.concat, then compare that approach with concatenating a complete data frame. section 2.12
df = pd.concat([df, new_products])
Inspect the concise operation that appends new_products to the existing data frame and returns the combined frame. section 2.10
