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 groups
are tuples use:
s = df2.explode('groups').set_index('groups')['ids']
df1['id'] = df1['col'].map(s)
temp_df2 = df2.set_index('ids')['groups'].apply(pd.Series).unstack().reset_index(name=1).drop('level_0',1)
df.merge(temp, on=1).drop(['level_0'],1)
Output
1 ids
0 A 1
1 B 3
2 C 2
3 D 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.