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