pandas append rows
DataFrame.append no longer exists: pandas 2.0 removed it, which is why older code fails with AttributeError. Add rows with pd.concat([df, new_rows], ignore_index=True), where new_rows is a DataFrame. For a single row on a default index, df.loc[len(df)] = [values] also works.
Syntax
pd.concat([df, other_df], ignore_index=True) | df.loc[len(df)] = [value1, value2]| Part | What it is |
|---|---|
ignore_index | True renumbers the rows 0, 1, 2 so the labels of the pieces do not repeat |
Examples
import pandas as pd
df = pd.DataFrame({"name": ["Ana", "Ben"], "age": [31, 27]})
new = pd.DataFrame([{"name": "Cy", "age": 40}])
pd.concat([df, new], ignore_index=True)Result:
name age
0 Ana 31
1 Ben 27
2 Cy 40The replacement for df.append(row, ignore_index=True).
df.loc[len(df)] = ["Di", 35]
dfResult:
name age
0 Ana 31
1 Ben 27
2 Di 35Adds one row in place. It relies on the index being 0, 1, 2 with no gaps.
rows = [{"name": "Eli", "age": 22}, {"name": "Flo", "age": 29}]
pd.concat([df, pd.DataFrame(rows)], ignore_index=True)Result:
name age
0 Ana 31
1 Ben 27
2 Di 35
3 Eli 22
4 Flo 29Collect the new rows in a list and add them in one concat.
Try it yourself
Stack jan on top of feb into one table with a fresh 0, 1, 2, ... index.
jan: 3 rows, first 4 shown
| day | sales |
|---|---|
| 1 | 120 |
| 2 | 95 |
| 3 | 140 |
feb: 4 rows, first 4 shown
| day | sales |
|---|---|
| 1 | 88 |
| 2 | 132 |
| 3 | 101 |
| 4 | 77 |
Free, no account needed. Real pandas runs in your browser; the first run takes a few seconds to load Python.
Common mistakes
- AttributeError: 'DataFrame' object has no attribute 'append': the code was written for pandas 1.x. Replace df.append(x) with pd.concat([df, x]).
- Calling concat inside a loop: each call copies the whole table, so it gets slower with every row. Append dicts to a Python list and build the DataFrame once at the end.
- Duplicate index labels after concat: without ignore_index=True the new rows keep their own labels, usually another 0.
More practice: adding rows (append)
These drills check your answer on live data, free, no account needed.
- Label the source with concat keysStack `jan` on top of `feb` with pd.concat, using the keys "jan" and "feb" so the outer index level says which month each row came from, and name that level `month`. Keep the original row labels as the inner level.
Questions
Why was append removed from pandas?
It copied the whole DataFrame on every call, which made loops very slow, and concat already did the same job. It was deprecated in 1.4 and removed in 2.0.
How do I add a row at the top?
Put the new row first: pd.concat([new, df], ignore_index=True).
How do I add a column instead of a row?
Assign it: df["new_col"] = values.
Related
- concatHow to use pd.concat in pandas to append rows from several DataFrames, add columns side by side with axis=1, reset the index, and label the source with keys.
- mergeHow to merge DataFrames in pandas like a SQL join: on a shared column, on different key names, left and outer joins, suffixes for clashing columns, validate.
- set_index and reset_indexHow to use set_index and reset_index in pandas: make a column the row index, go back to a plain column, drop the index, and flatten a groupby result.
- loc vs ilocThe difference between loc and iloc in pandas: loc selects by index label and column name, iloc by integer position. Slices, lists and boolean masks.
Interview and translation guides
- Timed mock interview
- pandas interview questions
- The pandas interview round
- SQL to pandas
- XLOOKUP in pandas
- pandas for Excel users
- groupby exercises