I have a Pandas DataFrame df
that store a matching between a label and an integer, and a Pandas Series s
that contains a sequence of labels :
print(df)
label id
0 AAAAAAAAA 0
1 BBBBBBBBB 1
2 CCCCCCCCC 2
3 DDDDDDDDD 3
4 EEEEEEEEE 4
print(s)
0 AAAAAAAAA
1 BBBBBBBBB
2 CCCCCCCCC
3 CCCCCCCCC
4 EEEEEEEEE
5 EEEEEEEEE
6 DDDDDDDDD
I want to join this DataFrame and this Series, to get the sequence of integer corresponding to my sequence s
. Here is the expected result of my example :
print(df.join(s)["id"])
0 0
1 1
2 2
3 2
4 4
5 4
6 3
Use Series.map
with Series
:
print (s.map(df.set_index('label')['id']))
0 0
1 1
2 2
3 2
4 4
5 4
6 3
Name: a, dtype: int64
Alternative - be careful, if dupes no error but return last dupe row:
print (s.map(dict(zip(df['label'], df['id']))))
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.