1

Introduction

We have the following dataframe which we create from a CSV file.

data = pd.read_csv(path + name, usecols = ['QTS','DSTP','RSTP','DDATE','RDATE','DTIME','RTIME','DCXR','RCXR','FARE'])

I want to delete specific rows from the dataframe. For this purpose I used a list and appended the ids of the rows we want to delete.

for index,row in data.iterrows():
     if (row['FARE'] >= 2500.00):
       indices.append(index)

From here i am lost. Don't know how to use the ids in the list to delete the rows from the dataframe


Question

  • The list containing the row ids must be used in the dataframe to delete rows. Is it possible to do it?

Constraints

  • We can't use data.drop(index,inplace=True) because it really slows the process
  • We cannot use a filter because I have some special constraints.

2 Answers 2

4

If you are trying to remove rows that have 'FARE' values greater than or equal to zero, you can use a mask that have those values lesser than 2500 -

df_out = df.loc[df.FARE.values < 2500] # Or df[df.FARE.values < 2500]

For large datasets, we might want to work with underlying array data and then construct the output dataframe -

df_out = pd.DataFrame(df.values[df.FARE.values < 2500], columns=df.columns)

To use those indices generated from the loopy code in the question -

df_out = df.loc[np.setdiff1d(df.index, indices)]

Or with masking again -

df_out = df.loc[~df.index.isin(indices)]  # or df[~df.index.isin(indices)]
Sign up to request clarification or add additional context in comments.

3 Comments

thank you @Divakar but is there a way to use the ids as i stated in the question?
@HaniGotc By ids, do you mean indices from the loop?
by using the list indices which contains the indices of the rows to delete
0

How about filtering data using DataFrame.query() method:

cols = ['QTS','DSTP','RSTP','DDATE','RDATE','DTIME','RTIME','DCXR','RCXR','FARE']
df = pd.read_csv(path + name, usecols=cols).query("FARE < 2500")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.