0

I have dataFrame object.

df = pd.read_csv("new_data.csv", index_col = 0) 

When I do

print df.head() 

The output was

 ... Risk ... 0 ... 2 ... 1 ... 3 ... ... 

But when I try this X = df.drop("Risk", 1).values

There was an error

"['Risk'] not found in axis"

5
  • 1
    pd.drop() has lots of arguments, try explicitly calling df.drop("Risk", axis=1, inplace=True) and then X = df.values in two separate commands as a troubleshooting approach. Commented Mar 7, 2019 at 13:10
  • 2
    You could have spaces in your column. maybe try df.columns.str.strip() before the drop Commented Mar 7, 2019 at 13:12
  • @MatthewArthur it doesnt work :( Commented Mar 7, 2019 at 13:13
  • @ecortazar Thanks Commented Mar 7, 2019 at 13:18
  • Are you sure that "Risk" is the name of a column instead of an entry in the first row? Commented Mar 7, 2019 at 13:29

1 Answer 1

1

If you know the position of your column (among the columns), you could try to delete your column by index.

Assuming that you want to delete column No 3 (count starts from 0, but does not include any index column), you can write:

df.drop(df.columns[3], axis=1) 

The above code is resistant to any "weird" or additional chars in column names.

Or maybe you should start from print(df.columns)? This will show you what are column names in your DataFrame.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.