To append a new row to an existing dataframe in pandas, you can use the append
method along with the loc
attribute.
First, create a dictionary with the data for the new row. For example:
main.py63 chars2 lines
Then, convert the dictionary to a dataframe using the pd.DataFrame.from_dict()
method:
main.py61 chars2 lines
Finally, append the new row to the existing dataframe using the append
method:
main.py61 chars2 lines
The ignore_index=True
parameter ensures that the index of the appended row is unique and does not overlap with existing indices. The append
method returns a new dataframe with the appended row, so you need to assign it back to the original dataframe variable.
Here's an example that puts these steps together:
main.py460 chars15 lines
This will output:
main.py108 chars5 lines
gistlibby LogSnag