简体   繁体   中英

How to encode labels in multiple pandas dataframes?

I have 2 dataframes df1 and df2

both have 3 columns:

c1, c2, c3

but in column c3 of df1 I have these unique labels:

l1, l2, l3, l4

And in column c3 of df2 I have these unique labels:

l2, l3, l4, l5

I want to encode these labels so last column values of df1 become like this:

1, 2, 3, 4

and df2:

2, 3, 4, 5

So the encoded labels should have the same name in both dataframes. The labels are not in order. And the uncommon labels might be more than 1.

Use below command:

>>> df1
   c3
0  l1
1  l2
2  l3
3  l4
>>> df1["c3"].apply(lambda x: x[-1])
0    1
1    2
2    3
3    4

Apply this to df2 also:

>>> df1["c3"].apply(lambda x: x[-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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM