Python. Pandas.Drop columnCalling an external command in PythonWhat are metaclasses in Python?How can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Convert bytes to a string?Converting integer to string in Python?Does Python have a string 'contains' substring method?Renaming columns in pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandas

Is expanding the research of a group into machine learning as a PhD student risky?

Why Were Madagascar and New Zealand Discovered So Late?

Applicability of Single Responsibility Principle

Sequence of Tenses: Translating the subjunctive

What is the opposite of 'gravitas'?

Shortcut for value of this indefinite integral?

Was a professor correct to chastise me for writing "Prof. X" rather than "Professor X"?

Is a stroke of luck acceptable after a series of unfavorable events?

Did Dumbledore lie to Harry about how long he had James Potter's invisibility cloak when he was examining it? If so, why?

How long to clear the 'suck zone' of a turbofan after start is initiated?

Why does indent disappear in lists?

What does "I’d sit this one out, Cap," imply or mean in the context?

Pole-zeros of a real-valued causal FIR system

How easy is it to start Magic from scratch?

Why not increase contact surface when reentering the atmosphere?

What is the intuitive meaning of having a linear relationship between the logs of two variables?

How to Reset Passwords on Multiple Websites Easily?

Why are there no referendums in the US?

How to be diplomatic in refusing to write code that breaches the privacy of our users

Method to test if a number is a perfect power?

when is out of tune ok?

Where does the Z80 processor start executing from?

How does Loki do this?

Is there a good way to store credentials outside of a password manager?



Python. Pandas.Drop column


Calling an external command in PythonWhat are metaclasses in Python?How can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Convert bytes to a string?Converting integer to string in Python?Does Python have a string 'contains' substring method?Renaming columns in pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandas













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"











share|improve this question

















  • 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.

    – Matthew Arthur
    Mar 7 at 13:10






  • 2





    You could have spaces in your column. maybe try df.columns.str.strip() before the drop

    – ecortazar
    Mar 7 at 13:12











  • @MatthewArthur it doesnt work :(

    – Samvel
    Mar 7 at 13:13











  • @ecortazar Thanks

    – Samvel
    Mar 7 at 13:18











  • Are you sure that "Risk" is the name of a column instead of an entry in the first row?

    – NoSplitSherlock
    Mar 7 at 13:29















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"











share|improve this question

















  • 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.

    – Matthew Arthur
    Mar 7 at 13:10






  • 2





    You could have spaces in your column. maybe try df.columns.str.strip() before the drop

    – ecortazar
    Mar 7 at 13:12











  • @MatthewArthur it doesnt work :(

    – Samvel
    Mar 7 at 13:13











  • @ecortazar Thanks

    – Samvel
    Mar 7 at 13:18











  • Are you sure that "Risk" is the name of a column instead of an entry in the first row?

    – NoSplitSherlock
    Mar 7 at 13:29













0












0








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"











share|improve this question














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"








python pandas dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 13:08









SamvelSamvel

1107




1107







  • 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.

    – Matthew Arthur
    Mar 7 at 13:10






  • 2





    You could have spaces in your column. maybe try df.columns.str.strip() before the drop

    – ecortazar
    Mar 7 at 13:12











  • @MatthewArthur it doesnt work :(

    – Samvel
    Mar 7 at 13:13











  • @ecortazar Thanks

    – Samvel
    Mar 7 at 13:18











  • Are you sure that "Risk" is the name of a column instead of an entry in the first row?

    – NoSplitSherlock
    Mar 7 at 13:29












  • 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.

    – Matthew Arthur
    Mar 7 at 13:10






  • 2





    You could have spaces in your column. maybe try df.columns.str.strip() before the drop

    – ecortazar
    Mar 7 at 13:12











  • @MatthewArthur it doesnt work :(

    – Samvel
    Mar 7 at 13:13











  • @ecortazar Thanks

    – Samvel
    Mar 7 at 13:18











  • Are you sure that "Risk" is the name of a column instead of an entry in the first row?

    – NoSplitSherlock
    Mar 7 at 13:29







1




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.

– Matthew Arthur
Mar 7 at 13:10





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.

– Matthew Arthur
Mar 7 at 13:10




2




2





You could have spaces in your column. maybe try df.columns.str.strip() before the drop

– ecortazar
Mar 7 at 13:12





You could have spaces in your column. maybe try df.columns.str.strip() before the drop

– ecortazar
Mar 7 at 13:12













@MatthewArthur it doesnt work :(

– Samvel
Mar 7 at 13:13





@MatthewArthur it doesnt work :(

– Samvel
Mar 7 at 13:13













@ecortazar Thanks

– Samvel
Mar 7 at 13:18





@ecortazar Thanks

– Samvel
Mar 7 at 13:18













Are you sure that "Risk" is the name of a column instead of an entry in the first row?

– NoSplitSherlock
Mar 7 at 13:29





Are you sure that "Risk" is the name of a column instead of an entry in the first row?

– NoSplitSherlock
Mar 7 at 13:29












1 Answer
1






active

oldest

votes


















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.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55044561%2fpython-pandas-drop-column%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer



























      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.






      share|improve this answer

























        1












        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 14:39









        Valdi_BoValdi_Bo

        5,4602916




        5,4602916





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55044561%2fpython-pandas-drop-column%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко