Pandas Dataframe method for deleting rows or columns

Source: Internet
Author: User

Pandas dataframe the additions and deletions of the summary series of articles:

    • How to create Pandas Daframe
    • Query method of Pandas Dataframe
    • Pandas Dataframe method for deleting rows or columns
    • Modification method of Pandas Dataframe

In this article we continue to introduce the relevant operations around Dataframe.

Usually in use dataframe time, delete operation with not too much, basically is to filter data from source Dataframe, compose a new dataframe and continue operation.

1. Delete a dataframe column

Here we continue to use the dataframe generated in the previous section to do an example, the original dataframe as follows:

We use the drop() function, this function has a list parameter labels, write can add labels=[xxx], also can not add, list to delete row or column name, default is the row name, if you want to delete the column, you want to add parameters axis=1 , the operation is as follows:

#pd.__version__ =='0.18.0'#drop columnstest_dict_df.drop(['id'],axis=1)#test_dict_df.drop(columns=['id']) # official operation, maybe my pandas version needs update!

The result is as follows, for the above code, the official tutorial document is given columns=[‘name‘] , but when I test the error, I use the Python3,pandas version of 0.18, may be the pandas version is too old for the sake of.

Note here that the result of the output is the result of the execution of this method, not the test_dict_df result of the output, because the default method is not to perform operations on its own, when the output test_dict_df output is still no delete operation of the original dataframe, if you want to operate on the original Dataframe, Needs to be added inplace=True , which is equivalent to assigning the value to itself after the operation:

test_dict_df.drop(['id'],axis=1,inplace=True)# test_dict_df = test_dict_df.drop(['id'],axis=1)
2. Delete a dataframe line

Delete a row, when the above Delete column operation is also slightly mentioned, if not Axis=1, the default is deleted by the line number, for example, to delete lines No. 0 and 4th:

test_dict_df.drop([0,4])

In the same vein, you have to add the InPlace parameter to the source Dataframe, otherwise it will not change on the TEST_DICT_DF.

Of course, if your dataframe has a lot of levels, you can add the level parameter, here is not much to repeat.

Pandas Dataframe method for deleting rows or columns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.