Pandas data processing

Source: Internet
Author: User
Pandas is a very important data processing library in Python, and pandas provides a very rich data processing function, which is helpful to machine learning and data preprocessing before data mining.

The following is the recent small usage summary: 1, pandas read the CSV file to obtain the Dataframe type object, which can enrich the execution of data processing. Missing value processing Dropna () or Fillna () 2, dataframe[' name ', ' age '] to get a specified number of columns of data is not possible, need two brackets df=df[[' name ', ' City '] 3, about the missing value of the processing before I summed up the missing value is an empty string, the processing is regular, in the pandas also provides a very friendly function map () to empty strings. 4, the processing of duplicate values can be drop_duplicates () processing 5, dataframe can be used Loc[row] to traverse the row, loc[row][' XXX ' to get the corresponding row number of field values, There is also a Boolean index Df[df.colname>value] 6, GroupBy provides a powerful split-apply-combine (grouping operation) function, can be based on the value of a column for the keyword to group the original data, The results of each grouping are obtained by traversing the grouped results (dataframe)

Groupdf=df.groupby (df[' Key1 '))
    for Name,group in groupdf:
        print Group  # end Group  dataframe type Object
        # Print Name    # name is a grouped keyword
7, Dataframe rebuild the index, sometimes we want to divide the group after each group has no row of the index or the original value, at this time we want to complete the group's current Dataframe reconstruction index, then we directly use Df.reindex will cause data loss

Raw data

The data after grouping

If you rebuild the indexed data with Reindex, you can see that the value is lost after the index changes

To resolve the Rebuild indexing method:
# Dataframe Rebuild Index
Group=group.reset_index (Drop=true)
GROUP=PD. Dataframe (Group)
8, Dataframe delete Row df=df.drop (Labels=row)

References: Jane book, blog

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.