How to delete a line of a data frame in python using pandas?
Question:
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
Answers:
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)
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 postion and size of the collection, indexes the wrong row and 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)
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 |
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)
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 postion and size of the collection, indexes the wrong row and 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)