Open In App

How to delete only one row in CSV with Python?

Prerequisites: pandas

One can open and edit CSV files in Python via Pandas library. While editing the file one might want to remove the entire row in the file. Following are some different approaches to do the same:



Data set in use: iris.csv dataset 

Method 1: Using slicing

This method is only good for removing the first or the last row from the dataset. And the following two lines of code which although means same represent the use of the .iloc[]  method in pandas.



Syntax:

df.iloc[<row_number>, <column_number>]

or

df.iloc[<row_number>]

Approach: To remove first row

(0 indexes will be removed as in python indexing starts from 0):

Program:




import pandas as pd
  
df = pd.read_csv(url)
  
df = df.iloc[1:]
  
print(df)

Output

removing first row

Approach: To remove the last row 

(here -1 represents the last row of the data)

Program:




import pandas as pd
  
df = pd.read_csv(url)
  
df = df.iloc[:-1]
  
print(df)

Output

removing last row

Method 2: Using drop() method

Removing using Label means the name of the row is specified in the code whereas using indexing means the index(position/ row number starting from 0) of the row is specified in the code.

Data set in use:

subset – top 5 items

Approach: Using row label

Program:




import pandas as pd
  
df = pd.read_csv(url)
  
# 2.
df_s = df[:5]
  
# 3.
df_s.set_index('sepal_length', inplace=True)
  
# 4.1.
df_s = df_s.drop(5.1)
  
print(df_s)

Output

using row label 

Approach: Using row index

df.index[ ] takes index numbers as a parameter starting from 1 and onwards whereas in python indexing starts from 0.

Program:




import pandas as pd
  
df = pd.read_csv(url)
  
df_s = df[:5]
  
df_s.set_index('sepal_length', inplace=True)
  
df_s = df_s.drop(df_s.index[1])
#df_s.drop(df_s.index[1],inplace = True)
  
print(df_s)

Output

using row index

Method 3: Removing using Conditions 

Dataset in use:

subset – top 5 items

Approach 1:

Program:




import pandas as pd
  
df = pd.read_csv(url)
  
  
df_s1 = df[:5]
  
  
df_s1 = df_s1.drop(df_s1[(df_s1.sepal_length == 4.7) &
                         (df_s1.petal_length == 1.3)].index)
  
print(df_s1)

Output

first method – conditional removal

                                  

Approach 2:

Program:




import pandas as pd
  
df = pd.read_csv(url)
  
df_s1 = df[:5]
  
df_s1 = df_s1.drop(df_s1.query('sepal_length==5.0').index)
  
print(df_s1)

Output:

second method – conditional removal

 


Article Tags :