Skip to content
Advertisement

Pandas: I want make a new column based on a series

What I want is just to add a column that copy the value of tmp with respect to serial number of c2 and map to c1.

JavaScript

Expected result:

JavaScript

The length of c1 sequence and c2 sequence are the same.

Longer sequence for reproduct:

JavaScript

Advertisement

Answer

Use Series.map with DataFrame.drop_duplicates, because c2 has duplicates:

JavaScript

Details:

JavaScript

Solution with merge:

JavaScript

EDIT: If need mapping duplicated sequences add GroupBy.cumcount for both DataFrames:

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement