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.
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]
:
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
unique SeriesGroupBy:
unique
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.
nunique
Source: https://habr.com/ru/post/1615646/More articles:Radio buttons with separate labels for listing using MVC6 tag helpers - enumsHow to make UIPopoverPresentationController translucent for iOS 9 - iosИзменение расширяет класс деятельности - javaCreate a set containing each member - delphiError translating mvel script to groovy - groovyUsing String in switch statement - c #SWTBot vs. unit testing - unit-testingCreating a string using a list of values - pythonWhat attribute should a good unit test have? - unit-testingHow to effectively test JUnit plug-ins Eclipse RCP - junitAll Articles