Python Forum
[Solved] Delete a line in a dataframe using .reset_index() and .drop()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Delete a line in a dataframe using .reset_index() and .drop()
#1
Hi out there,

I'd like to delete a line in the dataframe "df_new". Why does it not work? I get the error: "KeyError: '[0] not found in axis".

import pandas as pd import numpy as np data = {'x': [0,1, 2,3,4,5,6,7,8], 'y': [60, 23, 24,42,56,10,20,25,13], 'comment': ["false", "true","true","true","true","true","true","true","true"], } df = pd.DataFrame(data) df_new = df.loc[df['comment'] == "true"] # This works fine df = df.drop([0]) # This creates an error: KeyError: '[0] not found in axis' df_new = df_new.drop([0]) print(type(df)) # <class 'pandas.core.frame.DataFrame'> print(type(df_new)) # <class 'pandas.core.frame.DataFrame'>
Reply
#2
Indexing in pandas is not like indexing a list. Deleting a row does not change the index of the trailing rows. In your example df_new doesn't have an index[0].
You filtered out index[0] when when creating df_new. df_new starts at index[1]

You can reset the index using reset_index()

https://pandas.pydata.org/pandas-docs/st...index.html
import pandas as pd data = {'x': [0,1, 2,3,4,5,6,7,8], 'y': [60, 23, 24,42,56,10,20,25,13], 'comment': ["false", "true","true","true","true","true","true","true","true"], } df = pd.DataFrame(data) df_new = df.loc[df['comment'] == "true"] print(df_new) df_new = df_new.reset_index(drop=True) print(df_new) df_new = df_new.drop(0) print(df_new)
ju21878436312 likes this post
Reply
#3
Thank you very much! Dance
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Why does regex fail cleaning line? Winfried 7 4,854 Jul-11-2025, 11:52 PM
Last Post: Pedroski55
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 946 Apr-13-2025, 05:50 AM
Last Post: Winfried
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 4,050 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
  Delete empty text files [SOLVED] AlphaInc 5 4,398 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  how to populate a dataframe thru line iteration ? knob 0 1,745 May-05-2022, 12:48 AM
Last Post: knob
  Find and delete above a certain line in text file cubangt 12 8,966 Mar-18-2022, 07:49 PM
Last Post: snippsat
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 4,029 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  [Solved] Reading every nth line into a column from txt file Laplace12 7 8,944 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  [solved] unexpected character after line continuation character paul18fr 4 10,315 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  [solved] dataframe and read_csv ju21878436312 4 4,761 Jun-16-2021, 06:01 AM
Last Post: ju21878436312

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.