> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Insert a Row at Top of Pandas DataFrame
- URL: https://datascientyst.com/how-to-insert-a-row-at-top-of-pandas-dataframe/
- Published: 2025-04-25T13:59:55.000Z
- Updated: 2025-04-25T13:59:55.000Z
- Author: John D K
- Tags: Row

To insert a row at the top or a specific index on DataFrame you can achieve it bt using slicing or `concat`:

**(1) Using `pd.concat()` with a list**

```python
vals = [1, 2]
pd.concat([pd.DataFrame([vals], columns=df.columns), df], ignore_index=True)

```

**(2) Using `df.loc` with manual index shift and sort**

```python
df.loc[-1] = [1,2]
df.index = df.index + 1
df = df.sort_index()

```

## 1: Insert a Row on top of Pandas DataFrame

Let's say you have a simple DataFrame:

```python
import pandas as pd

df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie'],
    'age': [25, 30, 35]
})

```

If you want to insert a new row hen you can use the following syntax:

```python
vals = ['David', 28]
pd.concat([pd.DataFrame([vals], columns=df.columns), df], ignore_index=True)

```

Result:

|   | name    | age |
| - | ------- | --- |
| 0 | David   | 28  |
| 1 | Alice   | 25  |
| 2 | Bob     | 30  |
| 3 | Charlie | 35  |

### Insert at Position (e.g., Index 1)

```python
new_row = pd.DataFrame({'name': ['David'], 'age': [28]})
pd.concat([df.iloc[:1], new_row, df.iloc[1:]]).reset_index(drop=True)

```

Output:

|   | name    | age |
| - | ------- | --- |
| 0 | David   | 28  |
| 1 | Alice   | 25  |
| 2 | Bob     | 30  |
| 3 | Charlie | 35  |

### Tips

- Use `reset_index(drop=True)` after insertion to maintain continuous indexing.
- For appending to the end, use:  
  - `df.loc[len(df)] = vals` or
  - `df = pd.concat([df, new_row])`

## Resource

- [Pandas concat documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html?ref=datascientyst.com)
- [DataFrame.loc for assigning values](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html?ref=datascientyst.com)
- [Insert a row to pandas dataframe](https://stackoverflow.com/questions/24284342/insert-a-row-to-pandas-dataframe?ref=datascientyst.com)
- [How to concatenate multiple column values into a single column in Pandas dataframe](https://stackoverflow.com/questions/39291499/how-to-concatenate-multiple-column-values-into-a-single-column-in-pandas-datafra?ref=datascientyst.com)