Home > Blockchain >  How to delete a line of a data frame in python using pandas?
How to delete a line of a data frame in python using pandas?

Time:10-19

I want to delete the line that is in the loop.

I tried data.drop(), but it doesn't work

    for i,  receb in enumerate(data['recebimento']):
    
    if ordem == data.iloc[i, data.columns.get_loc('ordem')]:
        driver.find_element(By.XPATH,'//*::content"]').send_keys(str(imei))
        print(imei)
        data.drop(i)
codigo_produto recebimento orden imei
12d 123 4513512 ijam153
6412 171 45454 56656a

CodePudding user response:

Pandas drop method is not defaulted to perform the operation in place, but it returns a resulting dataframe; to change this, use the inplace flag. It is also good idea to specify the axis you're dropping; it defaults to 0 (Rows), but makes your code cleaner. Finally, your drop should look something like this:

data.drop(i, axis=0, inplace=True)

CodePudding user response:

It can be difficult to delete items from a collection you are also iterating. Rows get shifted by 1 and your index, still using the original size of the collection, eventually overflows. Also, iterating rows in pandas is relatively expensive. Since pandas is geared towards broadcasting an operation across the entire array, there is usually a better way to do it.

Here you can select the rows you want, perform operations and then drop the lot of them. I can't tell from your example what data you want to use from the dataframe, so this is just an estimate of the final code,

selected_imei = data[data["ordem"] == ordem]["imei"]
for imei in selected_imei:
    print("do stuff with", imei)
data.drop(selected_imei.index)
  • Related