Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to delete only one row in csv with Python?
In this tutorial, we will learn to delete only one row in CSV with Python using the Pandas library. Pandas is an open-source library for data analysis that provides several functionalities to perform operations on data sets.
We will use the drop() method to delete a row from any CSV file. This tutorial illustrates three different approaches to delete a row from CSV files using the same method.
Syntax
Here's the basic syntax to delete a row from a CSV file ?
import pandas as pd
# Read CSV file
df = pd.read_csv("filename.csv")
# Delete row by index
df = df.drop(df.index[index_number])
# Save updated data
df.to_csv("filename.csv", index=False)
In this syntax, we first read the DataFrame, then use the drop() method to remove the specified row by index, and finally save the updated data back to the CSV file.
Method 1: Delete Last Row
Here's how to delete the last row from a CSV file using the drop() method ?
import pandas as pd
# Create sample data
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 30, 35, 40],
'City': ['New York', 'London', 'Paris', 'Tokyo']
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# Delete the last row
df = df.drop(df.index[-1])
print("\nAfter deleting last row:")
print(df)
Original DataFrame:
Name Age City
0 Alice 25 New York
1 Bob 30 London
2 Charlie 35 Paris
3 David 40 Tokyo
After deleting last row:
Name Age City
0 Alice 25 New York
1 Bob 30 London
2 Charlie 35 Paris
Method 2: Delete Row by Index Label
When you have a specific column set as index, you can delete rows by their label ?
import pandas as pd
# Create sample data with custom index
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 30, 35, 40],
'City': ['New York', 'London', 'Paris', 'Tokyo']
}
df = pd.DataFrame(data, index=['row1', 'row2', 'row3', 'row4'])
print("Original DataFrame:")
print(df)
# Delete row by label
df = df.drop('row2')
print("\nAfter deleting 'row2':")
print(df)
Original DataFrame:
Name Age City
row1 Alice 25 New York
row2 Bob 30 London
row3 Charlie 35 Paris
row4 David 40 Tokyo
After deleting 'row2':
Name Age City
row1 Alice 25 New York
row3 Charlie 35 Paris
row4 David 40 Tokyo
Method 3: Delete Row with Condition
You can delete rows based on specific conditions using boolean indexing ?
import pandas as pd
# Create sample data
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Alice', 'David'],
'Age': [25, 30, 35, 28, 40],
'City': ['New York', 'London', 'Paris', 'Boston', 'Tokyo']
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# Delete rows where Name equals 'Alice' (only first occurrence)
condition = df['Name'] == 'Alice'
first_match_index = df[condition].index[0]
df = df.drop(first_match_index)
print("\nAfter deleting first occurrence of 'Alice':")
print(df)
Original DataFrame:
Name Age City
0 Alice 25 New York
1 Bob 30 London
2 Charlie 35 Paris
3 Alice 28 Boston
4 David 40 Tokyo
After deleting first occurrence of 'Alice':
Name Age City
1 Bob 30 London
2 Charlie 35 Paris
3 Alice 28 Boston
4 David 40 Tokyo
Comparison
| Method | Use Case | Syntax |
|---|---|---|
| By Position | Delete specific position (first, last, nth) | df.drop(df.index[-1]) |
| By Label | Delete by index label | df.drop('label') |
| By Condition | Delete based on column values | df.drop(df[condition].index[0]) |
Conclusion
The Pandas drop()
