Pandas Appending

Pandas appending is the process of adding new rows to a Pandas DataFrame. There are two main ways to append rows to a DataFrame:

Using the `append()` method

The `append()` method takes a DataFrame or Series object as input and appends it to the end of the calling DataFrame. The `ignore_index` parameter specifies whether or not to reset the index of the resulting DataFrame.

Here is an example of how to use the `append()` method to append a DataFrame to another DataFrame:

import pandas as pd

df1 = pd.DataFrame({'name': ['Alice', 'Bob', 'Carol'], 'age': [25, 30, 35]})
df2 = pd.DataFrame({'name': ['David', 'Eve', 'Frank'], 'age': [40, 45, 50]})

df1 = df1.append(df2, ignore_index=True)

print(df1)

Using the `concat()` method

The `concat()` method takes a list of DataFrame or Series objects as input and concatenates them along the specified axis. The `ignore_index` parameter specifies whether or not to reset the index of the resulting DataFrame.

Here is an example of how to use the `concat()` method to append a DataFrame to another DataFrame:

import pandas as pd

df1 = pd.DataFrame({'name': ['Alice', 'Bob', 'Carol'], 'age': [25, 30, 35]})
df2 = pd.DataFrame({'name': ['David', 'Eve', 'Frank'], 'age': [40, 45, 50]})

df1 = pd.concat([df1, df2], ignore_index=True)

print(df1)

Leave a Reply