Python Pandas: "isin" index of every match when multiple matches for the same index

Question:

Pretend the following dataframe:

|         | Col1 | Col2 |
|:---------|:------|:------|
| Index1  | 1055 | 2085 |
| Index2  | 1056 | 2086 |
| Index3  | 1057 | 2087 |
| Index4  | 1058 | 2088 |
| Index5  | 1059 | 2089 |
| Index6  | 1060 | 2090 |
| Index7  | 1061 | 2091 |
| Index8  | 1062 | 2092 |
| Index9  | 1063 | 2093 |
| Index10 | 1064 | 2094 |
| Index11 | 1065 | 2095 |
| Index12 | 1066 | 2096 |
| Index13 | 1067 | 2097 |
| Index14 | 1068 | 2098 |
| Index15 | 1069 | 2099 |
| Index16 | 1070 | 2100 |
| Index17 | 1071 | 2101 |
| Index18 | 1072 | 2102 |
| Index19 | 1073 | 2103 |
| Index20 | 1074 | 2104 |

And the following Python list:

| List |
|---|
| **1055** |
| **1055** |
| 1056 |
| 1057 |
| 1058 |
| 1059 |
| 1060 |
| 1061 |
| 1062 |
| 1063 |
| 1064 |
| 1065 |
| 1066 |
| 1067 |
| 1068 |
| 1069 |
| 1070 |
| 1071 |
| 1072 |
| 1073 |
| **1074** |
| **1074** |

Desired Output:

| Output |
|---|
| **Index1** |
| **Index1** |
| Index2 |
| Index3 |
| Index4 |
| Index5 |
| Index6 |
| Index7 |
| Index8 |
| Index9 |
| Index10 |
| Index11 |
| Index12 |
| Index13 |
| Index14 |
| Index15 |
| Index16 |
| Index17 |
| Index18 |
| Index19 |
| **Index20** |
| **Index20** |

I tried the following:

output = df.loc[df['Col1'].isin(list),:].index

Unfortunately, this only shows each index once. I manage to get the desired output, if I loop through every value and get the index of each value separately. However, this takes a lot of time.

Asked By: StatistikDude

||

Answers:

I think it’s worth searching by individual values, for example, like this:

output = [df.loc[df['Col1'] == list[i], :].index for i in range(len(list))]

Maybe this is not the best way, but it will definitely work))

Answered By: Vladislav Sokolov
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.