Skip to content
Advertisement

Map pandas dataframe columns to an array

I have a dataframe like this: Dataframe

JavaScript

And an array like:

JavaScript

The first element will be for family_id=0 and column “choice_0” = 52

The second element will be for family_id=1 and column “choice_2” = 82

The third element will be for family_id=2 and column “choice_4” = 27

And I will like to get:

JavaScript

The logic will be:

  • For family_id =0 The initial array has a a 52. And I will like to receive a 0 because it belongs to the “choice_0” column.
  • For family_id = 1 The initial array has a 82. And I will like to receive a 2 because it belongs to the “choice_2″column.

Note: Number within a row(family_id) can´t be repeated.

I don´t know even what is the title, feel free to change it.

Advertisement

Answer

Suppose you have:

JavaScript

You can get you desired output using a list comprehension.

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