Extract value of a particular column name in pandas as listed in another column
问题 The title wasn't too clear but here's an example. Suppose I have: person apple orange type Alice 11 23 apple Bob 14 20 orange and I want to get this column person new_col Alice 11 Bob 20 so we get the column 'apple' for row 'Alice' and 'orange' for row 'Bob'. I'm thinking iterrows, but that would be slow. Are there faster ways to do this? 回答1: Use DataFrame.lookup: df['new_col'] = df.lookup(df.index, df['type']) print (df) person apple orange type new_col 0 Alice 11 23 apple 11 1 Bob 14 20