Leo S
Leo S

Reputation: 11

Group pandas dataframe by value in one of several columns

Suppose I have a dataframe like this:

>>> ex = pd.DataFrame(
        {'from_id': [1, 2, 3], 'to_id': [2, 3, 1], 'duration': [5.0, 10.0, 15.0]}
    )

   from_id  to_id  duration
0        1      2       5.0
1        2      3      10.0
2        3      1      15.0

I'd like to access a sub-dataframe (or group) for each id, each containing all rows where either the from_id or the to_id is equal to that value. Here "duration" is just some other piece of data not being grouped by. Desired output would be

   from_id  to_id  duration
0        1      2       5.0
2        3      1      15.0

   from_id  to_id  duration
0        1      2       5.0
1        2      3      10.0

   from_id  to_id  duration
1        2      3      10.0
2        3      1      15.0

or similar. Is there a clean way to do this? Thanks!

Edit: An answer below suggests a way to grab all relevant rows given an ID. This does work, but ideally I'm looking to do this using a groupby expression, without first having to go through and create a separate array of unique IDs. And/or looking to maintain some sort of index from an id to the rows corresponding to it. New to pandas so open to ideas.

Upvotes: 1

Views: 42

Answers (1)

user2736738
user2736738

Reputation: 30906

ex[(ex['from_id']==id) | (ex['to_id']==id)]

You can simply do this for each id using boolean indexing. Idea is to if any of this condition is true it is part of my dataframe.

Upvotes: 1

Related Questions