Return value in dataframe based on row index, column reference

Aschharwood

My goal is to compare each value from the column "year" against the appropriate column year (i.e. 1999, 2000). I then want to return the corresponding value from the corresponding column. For example, for Afghanistan (first row), year 2004, I want to find the column named "2004" and return the value from the row that contains afghanistan.

Here is the table. For reference this table is the result of a sql join between educational attainment in a single defined year and a table for gdp per country for years 1999 - 2010. My ultimate goal is to return the gdp from the year that the educational data is from.

country year    men_ed_yrs  women_ed_yrs    total_ed_yrs    1999    2000    2001    2002    2003    2004    2005    2006    2007    2008    2009    2010
0   Afghanistan 2004    11  5   8   NaN NaN 2461666315  4128818042  4583648922  5285461999  6.275076e+09    7.057598e+09    9.843842e+09    1.019053e+10    1.248694e+10    1.593680e+10
1   Albania 2004    11  11  11  3414760915  3632043908  4060758804  4435078648  5746945913  7314865176  8.158549e+09    8.992642e+09    1.070101e+10    1.288135e+10    1.204421e+10    1.192695e+10
2   Algeria 2005    13  13  13  48640611686 54790060513 54744714110 56760288396 67863829705 85324998959 1.030000e+11    1.170000e+11    1.350000e+11    1.710000e+11    1.370000e+11    1.610000e+11
3   Andorra 2008    11  12  11  1239840270  1401694156  1484004617  1717563533  2373836214  2916913449  3.248135e+09    3.536452e+09    4.010785e+09    4.001349e+09    3.649863e+09    3.346317e+09
4   Anguilla    2008    11  11  11  NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

My approach so far is:

for value in df_combined_column_named['year']: #loops through each year in year column
    if value in df_combined_column_named.columns

any thoughts?

unutbu

Use df.loc:

In [62]: df.loc[df['country']=='Afghanistan', '2004'].item()
Out[62]: 5285461999.0

df.loc[rows, columns] can accept a boolean Series (such as df['country']=='Afghanistan') for rows and a column label (such as '2004') for columns. It will return the values for rows where the boolean Series is True and in the specified column.

In general this can be more than one value, so a Series is returned. However, in this case, there is only one value in the Series. So to obtain just the value, call the item method.


Note it is unclear from the posted string representation of df whether the numeric column labels are strings are integers. If the numeric column labels are integers, then you would need to use

df.loc[df['country']=='Afghanistan', 2004].item()

(with no quotation marks around 2004).


If you are going to make a lot of "queries" of this form, you make wish to set the country column as the index:

df = df.set_index('country')

Then you could access the value in the cell whose row label is 'Afghanistan' and whose column label is '2004' using get_value:

In [65]: df.get_value('Afghanistan', '2004')
Out[65]: 5285461999.0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Return value in dataframe based on row index, column reference

From Dev

iterate through df column and return value in dataframe based on row index, column reference

From Dev

iterate through df column and return value in dataframe based on row index, column reference

From Dev

How to return a dataframe value from row and column reference?

From Dev

Deleting DataFrame row in a multilevel index Pandas based on column value

From Dev

Return value pairs based on first row and column

From Java

Deleting DataFrame row in Pandas based on column value

From Dev

Get value from dataframe based on column and row

From Java

Create a column that assigns value to a row in a dataframe based on an event in another row

From Dev

C# WPF DataGrid Search for a Value in a Column, Return the Row Index

From Dev

For each row return the column index and name of non-NA value

From Dev

Return row of Data Frame based on value in a column - R

From Dev

Return unique row when using GROUP BY based on a column value

From Dev

Return one row in a view based on a left join column value

From Dev

Return row of Data Frame based on value in a column. R script

From Dev

Get Column and Row Index for Highest Value in Dataframe Pandas

From Dev

Find row-index of highest value in given column of dataframe

From Dev

Return row number(s) for a particular value in a column in a dataframe

From Dev

Pandas return index and column name based on the item value

From Dev

How to return the index of a column containing minimum value in a dataframe in R

From Dev

Index into dataframe by column value

From Java

How to delete a row in a dataframe based on a column event with consecutive same value

From Dev

pandas dataframe removing following row based on value in column

From Dev

Replace a row in a pandas DataFrame with a dict item based on a unique column value

From Dev

How to get value from python dataframe based on column index?

From Dev

Pandas dataframe remove rows based on index and column value

From Dev

return max value from panda dataframe as a whole, not based on column or rows

From Dev

How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe?

From Dev

Pandas Dataframe Add a value to a new Column based on the previous row limited to the maximum value in that column

Related Related

  1. 1

    Return value in dataframe based on row index, column reference

  2. 2

    iterate through df column and return value in dataframe based on row index, column reference

  3. 3

    iterate through df column and return value in dataframe based on row index, column reference

  4. 4

    How to return a dataframe value from row and column reference?

  5. 5

    Deleting DataFrame row in a multilevel index Pandas based on column value

  6. 6

    Return value pairs based on first row and column

  7. 7

    Deleting DataFrame row in Pandas based on column value

  8. 8

    Get value from dataframe based on column and row

  9. 9

    Create a column that assigns value to a row in a dataframe based on an event in another row

  10. 10

    C# WPF DataGrid Search for a Value in a Column, Return the Row Index

  11. 11

    For each row return the column index and name of non-NA value

  12. 12

    Return row of Data Frame based on value in a column - R

  13. 13

    Return unique row when using GROUP BY based on a column value

  14. 14

    Return one row in a view based on a left join column value

  15. 15

    Return row of Data Frame based on value in a column. R script

  16. 16

    Get Column and Row Index for Highest Value in Dataframe Pandas

  17. 17

    Find row-index of highest value in given column of dataframe

  18. 18

    Return row number(s) for a particular value in a column in a dataframe

  19. 19

    Pandas return index and column name based on the item value

  20. 20

    How to return the index of a column containing minimum value in a dataframe in R

  21. 21

    Index into dataframe by column value

  22. 22

    How to delete a row in a dataframe based on a column event with consecutive same value

  23. 23

    pandas dataframe removing following row based on value in column

  24. 24

    Replace a row in a pandas DataFrame with a dict item based on a unique column value

  25. 25

    How to get value from python dataframe based on column index?

  26. 26

    Pandas dataframe remove rows based on index and column value

  27. 27

    return max value from panda dataframe as a whole, not based on column or rows

  28. 28

    How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe?

  29. 29

    Pandas Dataframe Add a value to a new Column based on the previous row limited to the maximum value in that column

HotTag

Archive