Merging and Combining
Concat Rows
Stack two same-shaped record lists vertically by looping over each batch and
appending its rows into a combined list. With pandas, pd.concat([df1, df2])
stacks DataFrames along axis 0 (rows) and ignore_index=True resets the index
to a clean 0…n-1 range.
By hand
With pandas
pd.concat([df1, df2], ignore_index=True) stacks the two DataFrames
row-by-row. The snapshot shows the merged columns, shape, fresh index, and
the combined name column.
naive.py
batch1 = [
{'name': 'al', 'score': 80},
{'name': 'bo', 'score': 90},
]
batch2 = [
{'name': 'cy', 'score': 70},
{'name': 'di', 'score': 85},
]
combined = []
for r in batch1:
combined.append((r['name'], r['score']))
for r in batch2:
combined.append((r['name'], r['score']))
names = []
for t in combined:
names.append(t[0])
print('RESULT:', names)
library.py
import pandas as pd
from dalib.display import set_display
set_display()
df1 = pd.DataFrame({'name': ['al', 'bo'], 'score': [80, 90]})
df2 = pd.DataFrame({'name': ['cy', 'di'], 'score': [70, 85]})
combined = pd.concat([df1, df2], ignore_index=True)
print('columns:', combined.columns.tolist())
print('shape:', combined.shape)
print('index:', combined.index.tolist())
print('names:', combined['name'].tolist())
print('RESULT:', combined['name'].tolist())
columns: ['name', 'score']
shape: (4, 2)
index: [0, 1, 2, 3]
names: ['al', 'bo', 'cy', 'di']
RESULT: ['al', 'bo', 'cy', 'di']
Implementation notes
pd.concatdefaults toaxis=0(stack rows). Passaxis=1to stack columns side-by-side instead.- Without
ignore_index=True, the result keeps each DataFrame's original index. If both start at 0, the combined index has duplicate labels (0, 1, 0, 1), which can cause unexpected behavior in downstream.loclookups. pd.concataccepts any list of DataFrames — not just two. Pass a Python list of any length:pd.concat([df1, df2, df3, ...], ignore_index=True).- For appending a single new row,
pd.concat([df, new_row_df], ignore_index=True)is the modern replacement for the deprecateddf.append(row).