Insert a row to pandas dataframe
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Adding a row to a pandas DataFrame is common, but the "best" method depends on where the row should go and how often you are doing it. Pandas is column-oriented, so row insertion is not a special fast-path operation the way it might be in a spreadsheet.
Append a Row with loc
If your DataFrame uses a simple integer index and you want to add a row at the end, loc is usually the clearest option.
Output:
This works because len(df) is the next unused label when the index is 0, 1, 2, .... It is concise and readable for single-row appends.
You can also insert with a dictionary if you prefer matching by column name:
That is safer when the DataFrame has many columns and column order is easy to get wrong.
Insert a Row at a Specific Position with concat
There is no dedicated "insert row at position 2" API for DataFrames. The common pattern is to split the frame and concatenate the parts with a one-row DataFrame in the middle.
This is explicit and works well when row position matters for presentation or later processing. ignore_index=True tells pandas to rebuild the row labels from zero upward, which avoids duplicate or surprising index values.
Add Many Rows Efficiently
If you need to insert rows repeatedly, avoid growing the DataFrame one row at a time in a loop. That forces pandas to allocate new objects over and over again.
A better pattern is to collect rows first and concatenate once:
This scales much better and keeps the code simple. If the data already exists in Python objects, building a DataFrame from the full list is usually the cleanest approach.
Choose the Method Based on the Index
The index changes the meaning of insertion. With a default integer index, loc[len(df)] usually means "append to the end." With a custom index, loc uses labels instead of positions:
That is valid, but it is label assignment, not positional insertion. If you need a row to appear at a certain numeric position, concat with iloc slices is the clearer solution.
Common Pitfalls
- Using the old
appendhabit from older examples. Modern pandas code should preferconcat. - Assuming
loc[1]means "second row."locis label-based, not position-based. - Inserting a row with missing columns, which introduces
NaNvalues and sometimes changes dtypes. - Adding rows one by one inside a large loop, which is slow and memory-inefficient.
- Forgetting
ignore_index=Truewhen combining pieces, which can leave duplicate index labels behind.
Summary
- Use
df.loc[len(df)] = ...when you want to append one row to a DataFrame with a simple integer index. - Use
pd.concatwhen you need to insert a row at a specific position. - Build many rows in a list first and concatenate once for better performance.
- Remember that
locworks by label, whileilocworks by position. - Pick the method that matches your index semantics, not just the shortest syntax.
Related reading
- Insert element into numpy array and get all rolled permutations
- Insert or delete a step in scikit-learn Pipeline
- Insert result of sklearn CountVectorizer in a pandas dataframe
- Inserting image into IPython notebook markdown
- Insert at first position of a list in Python
- Insert to cassandra from python using cql
- Installation Issue with matplotlib Python
- Installing numpy on Docker Alpine
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.