Deleting DataFrame row in Pandas based on column value

0 votes

I have the following DataFrame:

             daysago  line_race rating        rw    wrating
 line_date                                                 
 2007-03-31       62         11     56  1.000000  56.000000
 2007-03-10       83         11     67  1.000000  67.000000
 2007-02-10      111          9     66  1.000000  66.000000
 2007-01-13      139         10     83  0.880678  73.096278
 2006-12-23      160         10     88  0.793033  69.786942
 2006-11-09      204          9     52  0.636655  33.106077
 2006-10-22      222          8     66  0.581946  38.408408
 2006-09-29      245          9     70  0.518825  36.317752
 2006-09-16      258         11     68  0.486226  33.063381
 2006-08-30      275          8     72  0.446667  32.160051
 2006-02-11      475          5     65  0.164591  10.698423
 2006-01-13      504          0     70  0.142409   9.968634
 2006-01-02      515          0     64  0.134800   8.627219
 2005-12-06      542          0     70  0.117803   8.246238
 2005-11-29      549          0     70  0.113758   7.963072
 2005-11-22      556          0     -1  0.109852  -0.109852
 2005-11-01      577          0     -1  0.098919  -0.098919
 2005-10-20      589          0     -1  0.093168  -0.093168
 2005-09-27      612          0     -1  0.083063  -0.083063
 2005-09-07      632          0     -1  0.075171  -0.075171
 2005-06-12      719          0     69  0.048690   3.359623
 2005-05-29      733          0     -1  0.045404  -0.045404
 2005-05-02      760          0     -1  0.039679  -0.039679
 2005-04-02      790          0     -1  0.034160  -0.034160
 2005-03-13      810          0     -1  0.030915  -0.030915
 2004-11-09      934          0     -1  0.016647  -0.016647

I need to remove the rows where line_race is equal to 0. What's the most efficient way to do this?

Jan 4, 2021 in Python by Roshni
• 10,520 points
2,064 views

2 answers to this question.

0 votes

If I'm understanding correctly, it should be as simple as:

df = df[df.line_race != 0]
answered Jan 4, 2021 by Gitika
• 65,910 points
0 votes

Pandas provide data analysts a way to delete and filter data frames using a data frames.drop() method. We can use this method to drop such rows that do not satisfy the given conditions.

Example 1: Delete rows based on condition on a column.

# import pandas library

import pandas as pd

  

# dictionary with list object in values

details = {

    'Name' : ['Ankit', 'Aishwarya', 'Shaurya',

              'Shivangi', 'Priya', 'Swapnil'],

    'Age' : [23, 21, 22, 21, 24, 25],

    'University' : ['BHU', 'JNU', 'DU', 'BHU', 

                    'Geu', 'Geu'],

}

  

# creating a Dataframe object 

df = pd.DataFrame(details, columns = ['Name', 'Age',

                                      'University'],

                  index = ['a', 'b', 'c', 'd', 'e', 'f'])

  

# get names of indexes for which

# column Age has value 21

index_names = df[ df['Age'] == 21 ].index

  

# drop these row indexes

# from dataFrame

df.drop(index_names, inplace = True)

  

df

Output :

python-pandas-drop-rows-2

answered Jan 4, 2021 by Nikita

Related Questions In Python

0 votes
0 answers

how to merge two data frames based on particular column in pandas python?

I want to  merge two data frames: df1 company,standard tata,A1 cts,A2 dell,A3 df2 company,return tata,71 dell,78 cts,27 hcl,23 I ...READ MORE

May 26, 2022 in Python by Kichu
• 19,050 points
240 views
0 votes
1 answer

Pandas row in printing whole structure instead of value.

Try this code, it should print only ...READ MORE

answered Mar 28, 2019 in Python by Siri
470 views
0 votes
1 answer

How to find if a value exists in Pandas dataframe?

Try this:​ for name in df['Name']: ...READ MORE

answered Apr 8, 2019 in Python by Tina
12,305 views
0 votes
1 answer

How to get value in Pandas dataframe using index?

You can use the iloc method to do ...READ MORE

answered Apr 8, 2019 in Python by Rohit
7,603 views
0 votes
1 answer

How to rename columns in pandas (Python)?

You can use the rename function in ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by MD 1,672 views
0 votes
1 answer

What is the Difference in Size and Count in pandas (python)?

The major difference is "size" includes NaN values, ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by Gitika 2,518 views
0 votes
2 answers

Replacing a row in pandas data.frame

key error. I love python READ MORE

answered Feb 18, 2019 in Data Analytics by anonymous
13,036 views
0 votes
1 answer

Converting a pandas data-frame to a dictionary

Emp_dict=Employee.to_dict('records') You can directly use the 'to_dict()' function ...READ MORE

answered May 23, 2018 in Data Analytics by Bharani
• 4,660 points
4,349 views
0 votes
1 answer

Delete column from pandas DataFrame in python

As you've guessed, the right syntax is del ...READ MORE

answered Dec 20, 2020 in Python by Gitika
• 65,910 points
741 views
0 votes
1 answer

Adding new column to existing DataFrame in Python pandas

Use the original df1 indexes to create ...READ MORE

answered Dec 23, 2020 in Python by Gitika
• 65,910 points
1,155 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP