0

I've received an error after trying to remove a column from my dataframe. KeyError: "['column name'] not found in axis" Advice is to put axis=1, but I did that and I still receive the same error. I don't know, what the problem is.

  
for i in range(1,len(file_list)):
    data = pd.read_table(file_list[i], encoding='unicode_escape')
    df = pd.DataFrame(data)
    main_dataframe = pd.concat([main_dataframe, df], axis = 1)

main_dataframe = main_dataframe.drop('column name',axis=1) 

    
1
  • There are a couple of things wrong here that are not directly related to your question but might be helpful: (1) Python starts indexing at 0 not 1, so your code will not be reading the first file. (2) You can just iterate through the file list for file_name in file_list: (3) calling pd.concat in a loop is inefficient, try appending all dataframes to a list first after reading and then calling pd.concat once at the end. Commented May 4, 2022 at 9:44

1 Answer 1

1

"column name" is a placeholder for your actual column name. For example, if you want to delete a column named "Surname", use:

df = df.drop("Surname",axis=1)

The error you have here is clearly stating that there is no column named "column name" in your dataframe.

Sign up to request clarification or add additional context in comments.

1 Comment

I was aware of that ^^ But thank you. I figuered it out, something else was wrong...

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.