简体   繁体   中英

Pandas - For a given column value in a row return the value from the column which name matches the value

For a given Pandas DataFrame row, how can I use the value from one column to return the value of another column which name is the value from the first column. Eg

if row['column_name'] == 'col_A':
    return row['col_A']
if row['column_name'] == 'col_B':
    return row['col_B']

The same way you would think of getting the max value when knowing the agrmax in numpy. Eg

   import pandas as pd
   import numpy as np
   df = pd.DataFrame({'col_A': [1,2,3], 'col_B': [2,3,0]})

   df['col_C-max'] = df.apply(np.max, axis = 1)
   df['col_D-colum_name_of_max_value'] = df[['col_A', 
   'col_B']].apply(np.argmax, axis = 1)

In this case getting col_C when knowing only the col_A, col_B, and col_D:

    col_A   col_B   col_C-max   col_D-colum_name_of_max_value
    0   1   2       2           col_B
    1   2   3       3           col_B
    2   3   0       3           col_A

You can use pandas 'fancy indexing' lookup , which pairs the index and the column names and pick up one value for each pair; and in this case it will be for each index(row), it picks up the value under the corresponding column from col_D :

df = pd.DataFrame({'col_A': [1,2,3], 'col_B': [2,3,0], 'col_D': ['col_B', 'col_B', 'col_A']})

df['col_C'] = df.lookup(df.index, df.col_D)

df
# col_A  col_B  col_D   col_C
#0    1      2  col_B   2
#1    2      3  col_B   3
#2    3      0  col_A   3

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