GroupBy operation of Pandas

Source: Internet
Author: User

This article and everyone to share is mainly pandasof theGroupByOperationRelated content, come together to look at it, hope to everyone learn pandas helpful.When doing data analysis, our data is generally from the database, then it involvesGroupByoperation. For example, if we want to forecast the electricity tariffs for a residential area for a certain period of time, then the data should be based on communityGroupBy, and then sort by time, hereGroupByThe operation is perfect for accomplishing this task. Hypothetical data tableCellfeeThe structure is: Reportdate, Cidyid, Cellid, fee. Reading table Data ImportPandas asPd fromSQLAlchemyImportcreate_engine# Default Engine = Create_engine (' Mysql+pymysql://ledao:[email protected]/pandas_learn ') Original_data = pd.read_sql_table (' Cellfee ', engine) Original_data groupby All data for a specified category in a grouped aggregate forKvinchOriginal_data.groupby ([original_data[' Cityid '], original_data[' Cellid ']): Print (' key: {}, type is {} '. Format (k, type (k))) Print (' vale:\n {}, \ntype is {} '. Format (V, type (v)))The result of the above code is: Key: (' 1 ', ' 1 '), type is Vale Reportdate Cityid cellid Fee 0 2017-07-20 1 1 10.0 1 2017-07-21 1 1 10.0 2 2017-07-22 1 1 10.0 3 2017-07-23 1 1 10.0, Type isthrough a simpleGroupByfunction, we will be able to summarize the data stored in the database into a group according to the grouping requirements into aDataFrame. Subsequent pairs of groupings form theDataFramecan do the formation of eigenvectors, sorting, and then continue to summarize the operation of common data analysis. in theGroupByon the operation, I only convincedScala,Kotlinthe model, i.e.GroupByPlusMap(FlatMap), hopingPandasThis functional operation will also be supported in the future. agg function exceptGroupByThe results are passed forafter the traversal,Pandasalso provides aaggfunction. Used primarily for the operation of specific columns, similar toSQL. Source:Jane Book

GroupBy operation of Pandas

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.