How can I select a specific column from each row in a Pandas DataFrame?

gggritso

I have a DataFrame in this format:

    a   b   c
0   1   2   3
1   4   5   6
2   7   8   9
3   10  11  12
4   13  14  15

and an array like this, with column names:

['a', 'a', 'b', 'c', 'b']

and I’m hoping to extract an array of data, one value from each row. The array of column names specifies which column I want from each row. Here, the result would be:

[1, 4, 8, 12, 14]

Is this possible as a single command with Pandas, or do I need to iterate? I tried using indexing

i = pd.Index(['a', 'a', 'b', 'c', 'b'])
i.choose(df)

but I got a segfault, which I couldn’t diagnose because the documentation is lacking.

DSM

You could use lookup, e.g.

>>> i = pd.Series(['a', 'a', 'b', 'c', 'b'])
>>> df.lookup(i.index, i.values)
array([ 1,  4,  8, 12, 14])

where i.index could be different from range(len(i)) if you wanted.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I "unpivot" specific columns from a pandas DataFrame?

From Dev

In rails, how to select one specific column's data from a table? (there is only 1 row so I want only a specific result but not an array)

From Dev

How can I select all DataFrame rows that are within a certain distance of a given value in a specific column?

From Dev

How can I convert each Pandas Data Frame row into an object including the column values as the attributes?

From Dev

How to construct pandas DataFrame from dictionary with key as column and row index

From Dev

Select specific index, column pairs from pandas dataframe

From Dev

Python Pandas : How to select two equal column per row of a dataframe

From Dev

How do I select from each column one specific index?

From Dev

pandas: how do I select first row in each GROUP BY group?

From Dev

How can I get the timezone adjusted hour for each row in a pandas DataFrame when working with multiple timezones?

From Dev

How can I retrieve a row by index value from a Pandas DataFrame?

From Dev

Pandas: How can I check if a pandas dataframe contains a specific value?

From Dev

How can I use the value of a cell in a row to chose find a column name in a pandas dataframe?

From Dev

Pandas: Select values from specific columns of a DataFrame by row

From Dev

How can I add a row or replace in a specific index in Pyspark Dataframe?

From Dev

How can I concatenate date from another column when I use groupby and aggregation in a pandas dataframe

From Dev

Remove a specific value from each row of a column

From Dev

Python/Pandas: How do I take specific columns from the second row of each ID and place it in new columns on the first row of the ID?

From Dev

How to plot each column with each column from Pandas Dataframe?

From Dev

Python Pandas - add column on a specific row, add specific row from one dataframe to another

From Dev

How can I search for specific text in a column of a pandas dataframe

From Dev

Pandas: How can I check if a pandas dataframe contains a specific value?

From Dev

How to prevent Pandas Dataframe from repeating column names at every row?

From Dev

How can I count features in each column of my dataframe ?

From Dev

How to efficiently subtract each row from pandas dataframe?

From Dev

How can I use jQuery to randomly select one column from each row?

From Dev

How to split a column in a dataframe and store each value as a new row (in pandas)?

From Dev

How can i select row from table according to column values of row in sql server

From Dev

How can I get value from Column A if select specific value from Column B in an Array?

Related Related

  1. 1

    How can I "unpivot" specific columns from a pandas DataFrame?

  2. 2

    In rails, how to select one specific column's data from a table? (there is only 1 row so I want only a specific result but not an array)

  3. 3

    How can I select all DataFrame rows that are within a certain distance of a given value in a specific column?

  4. 4

    How can I convert each Pandas Data Frame row into an object including the column values as the attributes?

  5. 5

    How to construct pandas DataFrame from dictionary with key as column and row index

  6. 6

    Select specific index, column pairs from pandas dataframe

  7. 7

    Python Pandas : How to select two equal column per row of a dataframe

  8. 8

    How do I select from each column one specific index?

  9. 9

    pandas: how do I select first row in each GROUP BY group?

  10. 10

    How can I get the timezone adjusted hour for each row in a pandas DataFrame when working with multiple timezones?

  11. 11

    How can I retrieve a row by index value from a Pandas DataFrame?

  12. 12

    Pandas: How can I check if a pandas dataframe contains a specific value?

  13. 13

    How can I use the value of a cell in a row to chose find a column name in a pandas dataframe?

  14. 14

    Pandas: Select values from specific columns of a DataFrame by row

  15. 15

    How can I add a row or replace in a specific index in Pyspark Dataframe?

  16. 16

    How can I concatenate date from another column when I use groupby and aggregation in a pandas dataframe

  17. 17

    Remove a specific value from each row of a column

  18. 18

    Python/Pandas: How do I take specific columns from the second row of each ID and place it in new columns on the first row of the ID?

  19. 19

    How to plot each column with each column from Pandas Dataframe?

  20. 20

    Python Pandas - add column on a specific row, add specific row from one dataframe to another

  21. 21

    How can I search for specific text in a column of a pandas dataframe

  22. 22

    Pandas: How can I check if a pandas dataframe contains a specific value?

  23. 23

    How to prevent Pandas Dataframe from repeating column names at every row?

  24. 24

    How can I count features in each column of my dataframe ?

  25. 25

    How to efficiently subtract each row from pandas dataframe?

  26. 26

    How can I use jQuery to randomly select one column from each row?

  27. 27

    How to split a column in a dataframe and store each value as a new row (in pandas)?

  28. 28

    How can i select row from table according to column values of row in sql server

  29. 29

    How can I get value from Column A if select specific value from Column B in an Array?

HotTag

Archive