Fadri
Fadri

Reputation: 157

Get data from multiindexed dataframe with given a list of index

I want to get information from from a column called 'index' in a certain pandas dataframe with multiple index. However, the index shall be listed. Please see the following example.

      index        ID
0  0    1.0  17226815
1  0    2.0  17226807
2  0    3.0  17226816   
3  0    4.0  17226808
4  0    5.0  17231739
5  0    6.0  17231739
6  0    1.0  17226815
   1    2.0  17226807
7  0    1.0  17226815
   1    3.0  17226816

filtered_list = [3, 5, 7]

with the following line I can get the filtered data.

print(df.loc[df.index.isin(filtered_list, level=0)]['index'])

out:

3  0    4.0
5  0    6.0
7  0    1.0
   1    3.0

what I want to get is a list consisting of the 'index' value. It will be then as additional information next to the filtered index. It is shown as follow:

0  3       4
1  5       6
2  7  (1, 3)

how can I get this list?

thank you in advance.

Upvotes: 1

Views: 32

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

If I understand correctly,

df.loc[filtered_list,'index'].groupby(level=0).apply(tuple).reset_index()

Output:

   0       index
0  3      (4.0,)
1  5      (6.0,)
2  7  (1.0, 3.0)

Going further:

df.loc[filtered_list,'index']\
  .groupby(level=0)\
  .apply(lambda x: tuple(x)[0] if len(x.index)==1 else tuple(x))\
  .reset_index()

OUtput:

   0       index
0  3           4
1  5           6
2  7  (1.0, 3.0)

Upvotes: 1

Related Questions