I have 2 dataframes, one containing some unique values, and another one containing unique groups of this unique values, with group ids, where each value appears in one group and one group only.
df1: df2:
groups ids
0 A 0 (A, D, F) 1
1 B 1 (C, E) 2
2 C 2 (B, K, L) 3
3 D .
. .
.
.
Is there an efficient way to map values from the first dataframe with ids from the second? I got the result using 2 for loops but it is very slow, and tried using 'np.where(df1 in df2["groups"])', but got an array of None.
Desired output:
df3:
id
0 A 1
1 B 3
2 C 2
3 D 1
.
.
.
If in column
groupsare tuples use nested dictionary comprehension withSeries.map:If data are Series: