How to delete multiple rows in csv file using python

‎05-10-2021 04:55 AM

Hi All,

I have data in csv file as shown in below image.

I want to remove only those rows whose column's values are 0.

For example : Channel and MS having 0,0 records in both columns , so just need to remove both rows(Channel and MS) from csv.

Please help on this.

Summary 5/14/2021 5/15/2021
individuals: 7983 7983
Channel 0 0
IR 0 2
MS 0 0
Duplicate 4339 4339
Channel2 0 5
unsuppressed 3643 3643

Thanks,

Neeraj

R provides a subset() function to delete or drop a single row and multiple rows from the DataFrame (data.frame), you can also use the notation [] and -c(). In this article, we will discuss several ways to delete rows from the data frame. We can delete rows from the data frame in the following ways:

  1. Delete Rows by Row Number from a data frame
  2. Delete Rows by Row Number
  3. Delete Multiple Rows from a data frame
  4. Delete Rows by Condition

Note that R doesn’t have a function that deletes the Rows from the R data frame however, we should use a subsetting way to drop rows. For example, to delete the second and third row in R, use -c(1, 3), and it will return the data frame without the first and third row.

1. Quick Examples of Delete Rows

Below are some quick examples of how to delete or drop rows from the R DataFrame.


#delete 4th row
df2 <- df[-4,]

#delete 4th,5th and 1st rows
df2 <- df[-c(4,5,1),]

# delete rows by range
df2 <- df[-(1:3),]

# delete rows by name
df2 <- df[!(row.names(df) %in% c("1","2")),]

#Remove rows with column id less than equal 2
df2 <- subset(df,id > 2 )

Let’s create a data frame with 5 rows and 3 columns.


#create dataframe with 5 rows and 3 columns
df = data.frame(id=c(1,2,3,4,5),
              name=c('sravan','srinu','chrisa','shivgami','jim'),
              gender=c('m','m','f','f','m'))

#display dataframe
df

Yields below output.


# Output
  id     name gender
1  1   sravan      m
2  2    srinu      m
3  3   chrisa      f
4  4 shivgami      f
5  5      jim      m

2. Delete Rows by Row Number from R Dataframe

In order to delete rows by row number from an R data frame (data.frame) using [] notation with the negative row index. Here, we are deleting only a single row from the R data frame using the row number. Row number starts with 1.

Syntax:


# Syntax
df[-row_index,]

Where df is the data frame from where you wanted to delete the row. Note that this doesn’t remove from the existing DataFrame so you need to assign it to another data frame to get the desired result.


#remove 4th row
df2 <- df[-4,]
df2

#remove 5th row
df2 <-df[-5,]
df2

#remove 1st row
df2 <-df[-1,]
df2

Output:


# Output
# Remove 4th Row
  id   name gender
1  1 sravan      m
2  2  srinu      m
3  3 chrisa      f
5  5    jim      m

# Remove 5th Row
  id     name gender
1  1   sravan      m
2  2    srinu      m
3  3   chrisa      f
4  4 shivgami      f

# Remove 1st Row
  id     name gender
2  2    srinu      m
3  3   chrisa      f
4  4 shivgami      f
5  5      jim      m

In the above code, we have deleted 4th, 5th, and 1st rows separately.

3. Delete Multiple Rows from R Dataframe

Use -c() with the row id you wanted to delete, Using this we can delete multiple rows at a time from the R data frame. Here row index numbers are specified inside vector c().

Syntax:


# Syntax
df[-c(row_number1,row_number2,.........),

Example:

In this example, we will delete multiple rows at a time.


#delete 4th,5th and 1st rows
df2 <- df[-c(4,5,1),]
df2

Output:


# Output
  id   name gender
2  2  srinu      m
3  3 chrisa      f

In the above code, we have deleted the 4th, 5th, and 1st rows at a time. so the output will be the remaining rows present in the data frame.

4. Delete Rows by Range

You can also delete rows by range using bracket notation. The following example drops all rows from row number 1 and 3.


# delete rows by range
df2 <- df[-(1:3),]
df2

Output:


# Output
  id     name gender
4  4 shivgami      f
5  5      jim      m

5. Delete Rows by Row Name

In our DataFrame we don’t have custom row names instead row names are the same as row numbers due to default behavior. If you have row names then you may need to delete rows by name. The below example demonstrates how you can do this.


# delete rows by name
df2 <- df[!(row.names(df) %in% c("1","2")),]
df2

Output:


# Output
  id     name gender
3  3   chrisa      f
4  4 shivgami      f
5  5      jim      m

6. Delete Rows by Condition

In this scenario, we are selecting only a particular row from a data frame based on the condition, by this way, we can ignore the rows from the data frame that fails the condition using the subset() method. This method takes two parameters data frame object and the condition.

Syntax:


# Syntax
subset(df,condition )

In this example, we will apply different conditions.


#Remove rows with column id less than equal 2
df2 <- subset(df,id > 2 )
df2

#Remove rows where gender not equal to 'm'
df2 <- subset(df,gender=='m' )
df2

Output:


# Output 1
  id     name gender
3  3   chrisa      f
4  4 shivgami      f
5  5      jim      m

# Output 2
  id   name gender
1  1 sravan      m
2  2  srinu      m
5  5    jim      m

In the above code

  • Output 1 – Selected rows based on the id column such that values in the row are greater than 2.
  • Output 2 – Selected rows based on gender column such that values in the row equal to ‘m’.

7. Conclusion

In this article, we have seen three methods to delete or drop single and multiple rows from the data frame in R language, Based on the requirement in your application, you can use any one of the above-discussed methods.

  • Sort R DataFrame Rows by Multiple Columns

References

  • https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/subset

How do you delete multiple rows in Python?

To delete rows and columns from DataFrames, Pandas uses the “drop” function. To delete a column, or multiple columns, use the name of the column(s), and specify the “axis” as 1. Alternatively, as in the example below, the 'columns' parameter has been added in Pandas which cuts out the need for 'axis'.

How do I delete a row in CSV?

Load data. Select required data. Find the row that specifies the specified condition using query() method. Use drop() method and pass the index of the fetched row as a parameter in the drop method.

How do I get rid of blank lines in Python CSV?

Use csv. Set newline to "" to prevent blank rows when writing. Call csv.

How do I delete a row in a Dataframe in Python?

To delete a row from a DataFrame, use the drop() method and set the index label as the parameter.