In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces Pandas how to use GroupBy grouping, the article is very detailed, has a certain reference value, interested friends must read it!
Groupby object import pandas as pdimport numpy as npdf = pd.DataFrame ({'A': ['foo',' bar', 'foo',' foo'],'B': ['one',' one', 'two',' three' 'two',' two', 'one',' three'],'C': np.random.randn (8) 'D': np.random.randn (8)}) gb.groupby ('A') print (df.groupby ('A')) In [26]: gb.gb.agg gb.boxplot gb.cummin gb.describe gb.filter gb.get_group gb.height gb.last gb.median gb.ngroups gb.plot gb.rank gb.std Gb.transformgb.aggregate gb.count gb.cumprod gb.dtype gb.first gb.groups gb.hist gb.max gb.min gb.nth gb.prod gb.resample gb.sum gb.vargb.apply gb.cummax gb.cumsum gb.fillna gb.gender gb.head gb.indices gb.mean gb.name gb. Ohlc gb.quantile gb.size gb.tail gb.weight grouping iteration Iterating through groupsIn [41]: grouped = df.groupby ('A') In [42]: for name Group in grouped:...: print (name)....: print (group)....: bar A B C D1 bar one-0.042379-0.0893293 bar three-0.009920-0.9458675 bar two 0.495767 1.956030foo A B C D0 foo one-0.919854-1.1313452 foo two 1. 247642 0.3378634 foo two 0.290213-0.9321326 foo one 0.362949 0.0175877 foo three 1.548106-0.016692 get a packet get_groupIn [44]: grouped.get_group ('bar') Out [44]: A B C D1 bar one-0.042379-0.0893293 bar three-0.009920-0.9458675 bar two 0.495767 1.956030 use the same function as agg () Number In [56]: grouped = df.groupby ('A') In [57]: grouped ['C'] .agg ([np.sum Np.mean, np.std]) Out [57]: sum mean stdA bar 0.443469 0.147823 0.301765foo 2.529056 0.505811 0.966450 different functions In [60]: grouped.agg ({'C': np.sum,....:'D': lambda x: np.std (x) Ddof=1)})....: Out [60]: C DA bar 0.443469 1.490982foo 2.529056 0.645875 transform data frame transformation
The transformation function (transform) needs to return a result of the same size as the packet block (group chunk), for example, we need to standardize the data for each packet:
In [66]: index = pd.date_range ('10 np.random.normal 1999, periods=1100) In [67]: ts = pd.Series (np.random.normal (0.5,2, 1100), index) In [68]: ts = ts.rolling (window=100) Min_periods=100) .mean () .dropna () In [71]: key = lambda x: x.year# use years to group In [72]: zscore = lambda x: (x-x.mean ()) / x.std () # standardized In [73]: transformed = ts.groupby (key). Transform (zscore) # use the year of the index to group Then standardize each group of data In [80]: compare = pd.DataFrame ({'Original': ts,' Transformed': transformed}) # to make a graph
Filter Filtration
The filter method returns a subset. For example, we only want groups whose group length is greater than 2:
In: dff = pd.DataFrame ({'Aids: np.arange (8)) List: list ('aabbbbcc')}) In [106]: dff.groupby (' B') .filter (lambda x: len (x) > 2) Out [106]: a B2 2b3 3b4 4b55b flexible use of applyIn: dfOut: A B C D0 foo one-0.919854-1.1313451 bar one-0.042379-0.0893292 foo two 1.247642 0.3378633 bar three -0.009920-0.9458674 foo two 0.290213-0.9321325 bar two 0.495767 1.9560306 foo one 0.362949 0.0175877 foo three 1.548106-0.016692In: grouped = df.groupby ('A') # could also just call. Apply (lambda x: x.describe ()) Out: A bar count 3.000000 mean 0.147823 std 0.301765 min -0.042379 25%-0.026149 50%-0.009920 75% 0.242924. Foo mean 0.505811 std 0.966450 min-0.919854 25% 0.290213 50% 0.362949 75% 1.247642 max 1.548106Name: C, dtype: float64 above is all the content of this article "how Pandas uses GroupBy grouping". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.