To append a new row to a Pandas DataFrame in Python, you can use the append()
function with the loc[]
or iloc[]
methods to specify the location of the new row.
Here is a sample code snippet that demonstrates how to append a new row to a DataFrame using the loc[]
method:
main.py306 chars14 lines
Output:
main.py64 chars5 lines
In this example, we first create a simple DataFrame with two rows using the pd.DataFrame()
function. Next, we define a new row as a Python dictionary with the same column names as the DataFrame. Finally, we use the loc[]
method with the len()
function to append the new row to the end of the DataFrame.
gistlibby LogSnag