Inserting and deleting rows to a DataFrame
In the following section, we will see how to insert and delete rows from a DataFrame
.
Getting ready
Run the following code to load NumPy, and pandas, and to initialize a DataFrame
object:
import numpy as np import pandas as pd df1 = pd.DataFrame([['Pen', 24, 2.39], ['Eraser', 32, 1.29], ['Sharpener', 12, 10.39], ['Pencil', 42, 0.59 ]], index=[100024, 201024, 202034, 101122], columns = ['item', 'inventory', 'unit_price'])
How to do it...
To insert rows at the end of the DataFrame
, run the code in the following cell:
df2 = pd.DataFrame([['Notepad', 12, 4.25], ['Binder', 8, 5.68]], index=[230015, 211040], columns = ['item', 'inventory', 'unit_price'] df3 = df1.append(df2)
To delete rows from a DataFrame
, use the following code:
df4 = df3.drop([230015, 211040])
How it works...
The append()
method appends a...