Grouping data in pandas?

I am new to python pandas and working with dataframes. Suppose I have a dataframe shown below:

A B C

3 2 3
4 2 4
3 2 1 
5 6 6

I want to find how many rows in my framework have the same value in column A and B, and for these rows I want to save the value of C. So, for example, in this framework I want to save the value of 1st and 3rd C, since A and B in each of these lines have the same meaning. Basically I want to print something like: "For A = 3 and B = 2, the possible values ​​are C: 3.1" and find these pairs. Im after the official pandas documentation, but I can not find it.

+4
source share
3 answers

Not sure if I will follow, but this may make you:

df = DataFrame({"a": [3,4,3,5], "b":[2,2,2,6], "c": [3,4,1,6]})
In [38]: for i, g in df.groupby(("a", "b")):
             print  i, g["c"].values
         ....:
(3, 2) [3 1]
(4, 2) [4]
(5, 6) [6]
+2

:

In [187]: df
Out[187]: 
   A  B  C
0  3  2  3
1  4  2  4
2  3  2  1
3  5  6  6

In [188]: df[df.groupby(['A', 'B']).transform(np.count_nonzero).C>=2]
Out[188]: 
   A  B  C
0  3  2  3
2  3  2  1
Name: C, dtype: float64
+2

unique SeriesGroupBy:

In [11]: df.groupby(["A", "B"])["C"].unique()
Out[11]:
A  B
3  2    [3, 1]
4  2       [4]
5  6       [6]
Name: C, dtype: object

See also nuniqueto get the number of unique items.

+2
source

Source: https://habr.com/ru/post/1615646/


All Articles