Add a row at top in pandas DataFrame
Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns).
Let’s see how can we can add a row at top in pandas DataFrame.
Observe this dataset first.
# importing pandas module import pandas as pd # making data frame df.head(10) |

Code #1: Adding row at the top of given datarame by concatenating the old dataframe with new one.
new_row = pd.DataFrame({'Name':'Geeks', 'Team':'Boston', 'Number':3, 'Position':'PG', 'Age':33, 'Height':'6-2', 'Weight':189, 'College':'MIT', 'Salary':99999}, index =[0]) # simply concatenate both dataframes df = pd.concat([new_row, df]).reset_index(drop = True) df.head(5) |
Output:

Code #2: Adding row at the top of given datarame by concatenating the old dataframe with new one.
new_row = pd.DataFrame({'Name':'Geeks', 'Team':'Boston', 'Number':3, 'Position':'PG', 'Age':33, 'Height':'6-2', 'Weight':189, 'College':'MIT', 'Salary':99999}, index =[0]) # Concatenate new_row with df df = pd.concat([new_row, df[:]]).reset_index(drop = True) df.head(5) |
Output:

Code #3: Adding row at the top of given datarame by concatenating the old dataframe with new one using df.ix[] method.
new_row = pd.DataFrame({'Name':'Geeks', 'Team':'Boston', 'Number':3, 'Position':'PG', 'Age':33, 'Height':'6-2', 'Weight':189, 'College':'MIT', 'Salary':99999}, index =[0]) df = pd.concat([new_row, df.ix[:]]).reset_index(drop = True) df.head(5) |
Output:

Recommended Posts:
- Python | Pandas DataFrame.fillna() to replace Null values in dataframe
- Python | Pandas dataframe.std()
- Python | Pandas dataframe.add()
- Python | Pandas DataFrame.where()
- Python | Pandas dataframe.cov()
- Python | Pandas dataframe.min()
- Python | Pandas dataframe.sem()
- Python | Pandas dataframe.sum()
- Python | Pandas DataFrame.abs()
- Python | Pandas dataframe.all()
- Python | Pandas dataframe.mad()
- Python | Pandas dataframe.div()
- Python | Pandas dataframe.mean()
- Python | Pandas dataframe.pow()
- Python | Pandas dataframe.max()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



