append a new line in a dataframe in python

To append a new line to an existing dataframe in Python using Pandas, you can use the append method. Here's an example code snippet:

main.py
import pandas as pd

# create a sample dataframe
data = {'name': ['Alice', 'Bob', 'Charlie'],
        'age': [25, 30, 35],
        'country': ['USA', 'Canada', 'UK']}
df = pd.DataFrame(data)

# add a new line to the dataframe
new_line = {'name': 'Dave', 'age': 40, 'country': 'Australia'}
df = df.append(new_line, ignore_index=True)

# print the updated dataframe
print(df)
374 chars
15 lines

In the above code, we first create a sample dataframe using a dictionary. Then, we define a new line as another dictionary. Finally, we append this new line to the existing dataframe using the append method and passing ignore_index=True to reset the index of the dataframe. Finally, we print the updated dataframe.

gistlibby LogSnag