AppDividend
Latest Code Tutorials

Python Pandas: How To Add Rows In DataFrame

0

Python Pandas dataframe append() is an inbuilt function that is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns, and the new cells are populated with NaN value.

How To Add Rows In DataFrame

Python Pandas DataFrame is a two-dimensional size-mutable, potentially composite tabular data structure with labeled axes (rows and columns). The DataFrame can contain the following types of data.

  1. The Pandas Series: One-dimensional labeled array capable of holding any data type with axis labels or index. An example of the Series object is one column from the DataFrame.
  2. NumPy ndarray, which can be the record or structure.
  3. The two-dimensional ndarray using NumPy.
  4. Dictionaries of one-dimensional ndarray’s, listsdictionaries, or Series.

There are mainly three methods we can use to add rows in DataFrame.

  1. Dataframe.append()
  2. loc[ ]
  3. iloc[ ]

Pandas Dataframe.append()

DataFrame.append() is an inbuilt function that is used to merge rows from another DataFrame object. The append() function returns the new DataFrame object and doesn’t change the source objects.

If there is a mismatch in the columns, the new columns are added in the result DataFrame.

See the following syntax.

DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None)

Parameters

  1. other: DataFrame or Series/dict-like object, or list.
  2. ignore_index: If True, do not use the index labels.
  3. verify_integrity: If True, raise ValueError on creating an index with duplicates.
  4. sort: Sort columns if the columns of self and other are not aligned. The default sorting is deprecated and will change to not-sorting in the future version of pandas. Explicitly pass sort=True to silence the warning and sort. Explicitly pass sort=False to silence the warning and not sort.

Okay, so let’s see the above function in action.

Create Pandas dataframe using a list of tuples

See the following code.

# app.py

import pandas as pd

shows = [('The Witcher', 'Henry Cavil', 'Geralt'),
         ('Stranger Things', 'Millie Brown', 'Eleven'),
         ('BoJack Horseman', 'Will', 'BoJack'),
         ('Adventures of Sabrina', 'Kiernan Shipka', 'Spellman'),
         ('House of Cards', 'Kevin Spacey', 'Frank Underwood')]

dfObj = pd.DataFrame(shows,
                     columns=['Series', 'Name', 'Character Name'])

print(dfObj)

In the above code, we have used the shows list, which consists of five tuples.

The column names of dataframe are Series, Name, and Character Name.

The index of dataframe is a, b, c, d, e.

See the output of the above code.

python3 app.py
                  Series            Name   Character Name
0            The Witcher     Henry Cavil           Geralt
1        Stranger Things    Millie Brown           Eleven
2        BoJack Horseman            Will           BoJack
3  Adventures of Sabrina  Kiernan Shipka         Spellman
4         House of Cards    Kevin Spacey  Frank Underwood

Add row in dataframe using dataframe.append() and dict

In dataframe.append() we can pass a dictionary of key-value pairs i.e.

  • key = Column name
  • Value = Value at that column in the new row

Let’s add the new row in above dataframe bypassing dictionary i.e.

# app.py

newDfObj = dfObj.append({'Series': 'S Education',
                         'Name': 'ASA',
                         'Character Name': 'Otis'}, ignore_index=True)

print(newDfObj)

In the above code, we have called the append() function of the dataframe object and pass the dictionary as a new row of dataframe.

See the full code below.

# app.py

import pandas as pd

shows = [('The Witcher', 'Henry Cavil', 'Geralt'),
         ('Stranger Things', 'Millie Brown', 'Eleven'),
         ('BoJack Horseman', 'Will', 'BoJack'),
         ('Adventures of Sabrina', 'Kiernan Shipka', 'Spellman'),
         ('House of Cards', 'Kevin Spacey', 'Frank Underwood')]

dfObj = pd.DataFrame(shows,
                     columns=['Series', 'Name', 'Character Name'])

newDfObj = dfObj.append({'Series': 'S Education',
                         'Name': 'ASA',
                         'Character Name': 'Otis'}, ignore_index=True)

print(newDfObj)

Output

python3 app.py
                  Series            Name   Character Name
0            The Witcher     Henry Cavil           Geralt
1        Stranger Things    Millie Brown           Eleven
2        BoJack Horseman            Will           BoJack
3  Adventures of Sabrina  Kiernan Shipka         Spellman
4         House of Cards    Kevin Spacey  Frank Underwood
5            S Education             ASA             Otis

From the output, you can see that the last row is the newly added row.

So, this is how you add the row in the pandas dataframe.

For columns that were not present in the dictionary, NaN value is added in the final dataframe.

Let’s see that scenario.

# app.py

newDfObj = dfObj.append({'Series': 'S Education',
                         'Name': 'ASA'}, ignore_index=True)

print(newDfObj)

In the above code, we have skipped the Character Name while adding a new row. So in the output, NaN will be returned. See the output.

python3 app.py
                  Series            Name   Character Name
0            The Witcher     Henry Cavil           Geralt
1        Stranger Things    Millie Brown           Eleven
2        BoJack Horseman            Will           BoJack
3  Adventures of Sabrina  Kiernan Shipka         Spellman
4         House of Cards    Kevin Spacey  Frank Underwood
5            S Education             ASA              NaN

Passing the ignore_index=True is necessary while passing the dictionary or series otherwise following TypeError error will come for example,

“TypeError: Can only append a Series if ignore_index=True or if the Series has a name.”

Add row in the dataframe using dataframe.append() and Series

We can also pass the series to append() to append a new row in the dataframe.

See the following code.

# app.py

newDfObj = dfObj.append(pd.Series(['S Education', 'ASA', 'Otis'],
                                  index=dfObj.columns),
                        ignore_index=True)

print(newDfObj)

In the above code, we have added a series as a new row, which has three values.

See the full code below.

# app.py

import pandas as pd

shows = [('The Witcher', 'Henry Cavil', 'Geralt'),
         ('Stranger Things', 'Millie Brown', 'Eleven'),
         ('BoJack Horseman', 'Will', 'BoJack'),
         ('Adventures of Sabrina', 'Kiernan Shipka', 'Spellman'),
         ('House of Cards', 'Kevin Spacey', 'Frank Underwood')]

dfObj = pd.DataFrame(shows,
                     columns=['Series', 'Name', 'Character Name'])

newDfObj = dfObj.append(pd.Series(['S Education', 'ASA', 'Otis'],
                                  index=dfObj.columns),
                        ignore_index=True)

print(newDfObj)

Output

python3 app.py
                  Series            Name   Character Name
0            The Witcher     Henry Cavil           Geralt
1        Stranger Things    Millie Brown           Eleven
2        BoJack Horseman            Will           BoJack
3  Adventures of Sabrina  Kiernan Shipka         Spellman
4         House of Cards    Kevin Spacey  Frank Underwood
5            S Education             ASA             Otis

So, we get the same output as we have added one dictionary as a row in the dataframe.

How to add multiple rows in the dataframe using dataframe.append() and Series

Until now, we have added a single row in the dataframe. Now, we will add multiple rows in the dataframe using dataframe.append() and pandas series.

We can pass the list of series in dataframe.append() for appending multiple rows in the dataframe.

So, let’s create a list of series with the same column names as the dataframe.

See the following code.

# app.py

listOfSeries = [pd.Series(['S Education', 'ASA', 'Otis'], index=dfObj.columns),
                pd.Series(['Money Heist', 'Ursu Lolita', 'Tokyo'],
                          index=dfObj.columns),
                pd.Series(['Black Mirror', 'Miley Cyrus', 'Ashley'], index=dfObj.columns)]

newDfObj = dfObj.append(listOfSeries, ignore_index=True)

See the following complete code.

# app.py

import pandas as pd

shows = [('The Witcher', 'Henry Cavil', 'Geralt'),
         ('Stranger Things', 'Millie Brown', 'Eleven'),
         ('BoJack Horseman', 'Will', 'BoJack'),
         ('Adventures of Sabrina', 'Kiernan Shipka', 'Spellman'),
         ('House of Cards', 'Kevin Spacey', 'Frank Underwood')]

dfObj = pd.DataFrame(shows,
                     columns=['Series', 'Name', 'Character Name'])

listOfSeries = [pd.Series(['S Education', 'ASA', 'Otis'], index=dfObj.columns),
                pd.Series(['Money Heist', 'Ursu Lolita', 'Tokyo'],
                          index=dfObj.columns),
                pd.Series(['Black Mirror', 'Miley Cyrus', 'Ashley'], index=dfObj.columns)]

newDfObj = dfObj.append(listOfSeries, ignore_index=True)

print(newDfObj)

Output

python3 app.py
                  Series            Name   Character Name
0            The Witcher     Henry Cavil           Geralt
1        Stranger Things    Millie Brown           Eleven
2        BoJack Horseman            Will           BoJack
3  Adventures of Sabrina  Kiernan Shipka         Spellman
4         House of Cards    Kevin Spacey  Frank Underwood
5            S Education             ASA             Otis
6            Money Heist     Ursu Lolita            Tokyo
7           Black Mirror     Miley Cyrus           Ashley

Add a row from one dataframe to other dataframe using pandas dataframe append()

Let’s create another dataframe.

# app.py

shows2 = [('S Education', 'ASA', 'Otis'),
          ('Money Heist', 'Ursu Lolita', 'Tokyo'),
          ('Black Mirror', 'Miley Cyrus', 'Ashley')]

dfObj2 = pd.DataFrame(shows2,
                      columns=['Series', 'Name', 'Character Name'])

Now, append the dfObj2 dataframe object to the dfObj dataframe object.

# app.py

newDfObj = dfObj.append(dfObj2, ignore_index=True)

See the following complete code.

# app.py

import pandas as pd

shows = [('The Witcher', 'Henry Cavil', 'Geralt'),
         ('Stranger Things', 'Millie Brown', 'Eleven'),
         ('BoJack Horseman', 'Will', 'BoJack'),
         ('Adventures of Sabrina', 'Kiernan Shipka', 'Spellman'),
         ('House of Cards', 'Kevin Spacey', 'Frank Underwood')]

dfObj = pd.DataFrame(shows,
                     columns=['Series', 'Name', 'Character Name'])

shows2 = [('S Education', 'ASA', 'Otis'),
          ('Money Heist', 'Ursu Lolita', 'Tokyo'),
          ('Black Mirror', 'Miley Cyrus', 'Ashley')]

dfObj2 = pd.DataFrame(shows2,
                      columns=['Series', 'Name', 'Character Name'])

newDfObj = dfObj.append(dfObj2, ignore_index=True)

print(newDfObj)

Output

python3 app.py
                  Series            Name   Character Name
0            The Witcher     Henry Cavil           Geralt
1        Stranger Things    Millie Brown           Eleven
2        BoJack Horseman            Will           BoJack
3  Adventures of Sabrina  Kiernan Shipka         Spellman
4         House of Cards    Kevin Spacey  Frank Underwood
5            S Education             ASA             Otis
6            Money Heist     Ursu Lolita            Tokyo
7           Black Mirror     Miley Cyrus           Ashley

We will get the same output as above, but this time, we have added one dataframe as multiple rows to another dataframe.

Add a row in the dataframe using loc[] & list

We can add a new row using loc[ ] and list.

# app.py

dfObj = pd.DataFrame(shows,
                     columns=['Series', 'Name', 'Character Name'])

dfObj.loc[5] = ['S Education', 'ASA', 'Otis']

We are adding a new row in the dataframe at index 5.

See the full code.

# app.py

import pandas as pd

shows = [('The Witcher', 'Henry Cavil', 'Geralt'),
         ('Stranger Things', 'Millie Brown', 'Eleven'),
         ('BoJack Horseman', 'Will', 'BoJack'),
         ('Adventures of Sabrina', 'Kiernan Shipka', 'Spellman'),
         ('House of Cards', 'Kevin Spacey', 'Frank Underwood')]

dfObj = pd.DataFrame(shows,
                     columns=['Series', 'Name', 'Character Name'])

dfObj.loc[5] = ['S Education', 'ASA', 'Otis']

print(dfObj)

If the dataframe already had any row with index name 5, then this will replace the contents of that row. Otherwise, it will add a new row.

Output

python3 app.py
                  Series            Name   Character Name
0            The Witcher     Henry Cavil           Geralt
1        Stranger Things    Millie Brown           Eleven
2        BoJack Horseman            Will           BoJack
3  Adventures of Sabrina  Kiernan Shipka         Spellman
4         House of Cards    Kevin Spacey  Frank Underwood
5            S Education             ASA             Otis

Add a row in dataframe at index position using iloc[]

We can add a new row in the dataframe at a particular index position using iloc[].

# app.py

dfObj.iloc[2] = ['S Education', 'ASA', 'Otis']

See the following code.

# app.py

import pandas as pd

shows = [('The Witcher', 'Henry Cavil', 'Geralt'),
         ('Stranger Things', 'Millie Brown', 'Eleven'),
         ('BoJack Horseman', 'Will', 'BoJack'),
         ('Adventures of Sabrina', 'Kiernan Shipka', 'Spellman'),
         ('House of Cards', 'Kevin Spacey', 'Frank Underwood')]

dfObj = pd.DataFrame(shows,
                     columns=['Series', 'Name', 'Character Name'])

dfObj.iloc[2] = ['S Education', 'ASA', 'Otis']

print(dfObj)

It will replace the row at index 2. So in our example, it will replace the BoJack Horseman.

Output

python3 app.py
                  Series            Name   Character Name
0            The Witcher     Henry Cavil           Geralt
1        Stranger Things    Millie Brown           Eleven
2            S Education             ASA             Otis
3  Adventures of Sabrina  Kiernan Shipka         Spellman
4         House of Cards    Kevin Spacey  Frank Underwood

Conclusion

Python Pandas dataframe append() function is used to add single series, dictionary, dataframe as a row in the dataframe. We can add multiple rows as well.

We can also use loc[ ] and iloc[ ] to modify an existing row or add a new row.

Finally, Python Pandas: How To Add Rows In DataFrame is over.

See also

Pandas set_index()

Pandas boolean indexing

Pandas sort_values()

Pandas value_counts()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.