how to append at the end of a dataframe in python

To append rows at the end of a dataframe in Python using Pandas library, you can use the "append" method. Here's an example:

main.py
import pandas as pd

# Create an empty dataframe
df = pd.DataFrame(columns=['col1', 'col2', 'col3'])

# Append a row to the dataframe
df = df.append({'col1': 'value1', 'col2': 'value2', 'col3': 'value3'}, ignore_index=True)

# Append multiple rows to the dataframe using a list of dictionaries
rows_list = [{'col1': 'value4', 'col2': 'value5', 'col3': 'value6'},
             {'col1': 'value7', 'col2': 'value8', 'col3': 'value9'}]
df = df.append(rows_list, ignore_index=True)

# Print the updated dataframe
print(df)
518 chars
16 lines

This will output:

main.py
    col1   col2   col3
0  value1  value2  value3
1  value4  value5  value6
2  value7  value8  value9
101 chars
5 lines

The "ignore_index" parameter set to "True" ensures that the appended rows get new index values instead of continuing from the last existing index.

gistlibby LogSnag