Reputation: 5031
I have the following table
Label B C
1 5 91
1 5 65
1 5 93
-1 5 54
-1 5 48
1 10 66
1 10 54
-1 10 15
I want only those values in C which are labeled '1' for each set of values in B.I want to extract those values from C in a list like this:
[[91 65 93],[66 54]]
Implementing similar thing in python is easy but i want to do same thing using pandas.
Upvotes: 2
Views: 4792
Reputation: 85615
Not so nice as other answers:
first select label and get useful columns:
df2 = df[df['Label'] == 1][['B','C']].set_index('B')
Then just a list comprehension to get values
print [list(df2.ix[index]['C']) for index in set(df2.index)]
you get:
[[66, 54], [91, 65, 93]]
Upvotes: 1
Reputation: 1286
You can groupby the Label column, apply the list constructor. Here is an minimal example.
Label = [1, 1, 1, -1, -1, -1]
c = [91, 65, 93, 54, 48, 15]
df = pd.DataFrame({'Label': Label, 'c': c})
df['c'].groupby(df['Label']).apply(list)[1] # Change 1 to -1 if you want the -1 group
If you only want unique entries, then you can do
df['c'].groupby(df['Label']).unique()[1]
Upvotes: 1
Reputation: 393903
You can filter the df to just those values where Label is 1, then on the remaining columns groupby B and get the unique values of C:
In [26]:
gp = df[df['Label']==1][['B','C']].groupby('B')
gp['C'].unique()
Out[26]:
B
5 [91, 65, 93]
10 [66, 54]
Name: C, dtype: object
You can convert it to a list of arrays also:
In [36]:
list(gp['C'].unique().values)
Out[36]:
[array([91, 65, 93], dtype=int64), array([66, 54], dtype=int64)]
Upvotes: 4