Reputation: 199
I'm pretty new to python pandas and working with dataframes. Lets say that I have the 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 dataframe have the same value in the A and B column and for these rows I want to store the C value. So for example in this dataframe, I want to store the 1st and 3rd C value, since the A and B in each of these rows have the same values. Basically I want a way to print something like: "For A = 3 and B = 2 the possible C values are : 3,1" and find those pairs. Im following the official pandas documentation but I can't seem to find this.
Upvotes: 4
Views: 107
Reputation: 375377
You can use the unique
SeriesGroupBy method:
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 nunique
to get the number of unique elements.
Upvotes: 2
Reputation: 1136
You may try this:
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
Upvotes: 2
Reputation: 10397
Not sure if I follow but this might get you going:
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]
Upvotes: 2