0

This is my csv file:

A  B  C  D
0  1  5  5
1  0  3  0
0  0  0  0
2  1  3  4 

I want it check The B column if I foud 0 I delete all the row so this what I need as output:

A  B  C  D
0  1  5  5
2  1  3  4

I tried this code :

import pandas as pd
df=pd.read_csv('Book1.csv', sep=',', error_bad_lines=False, dtype='unicode')
for index, row in df.iterrows():
    if row['B'] == 0:
        df.drop(row,index=False)
df.to_csv('hello.csv')

It return for me :

   A  B  C  D
0  0  1  5  5
1  1  0  3  0
2  0  0  0  0
3  2  1  3  4

It did not delete any thing I don't know where is the problem Any help please !

yatu
  • 86,083
  • 12
  • 84
  • 139
  • You didn't drop `inplace` but anyway, you shouldn't be iterating. `df = df[df['B'] != 0]` – roganjosh Jul 10 '19 at 15:18
  • 2
    Possible duplicate of [Select rows from a DataFrame based on values in a column in pandas](https://stackoverflow.com/questions/17071871/select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas) – sjw Jul 10 '19 at 15:18

1 Answers1

2

You could check which rows in B are not equal to 1, and perform boolean indexing with the result:

df[df.B.ne(0)]

   A  B  C  D
0  0  1  5  5
3  2  1  3  4

Note that in your approach, in order to drop a given row, you need to specify the index, so you should be doing something like:

for index, row in df.iterrows():
    if row['B'] == 0:
        df.drop(index, inplace=True)
df.to_csv('hello.csv')

Also don't forget to reassign the result after dropping a row. This can be done setting inplace to True or reassigning back to df.

yatu
  • 86,083
  • 12
  • 84
  • 139