1

I have a dataframe with many columns and one row, and need to get the column headers for every time that a specific value appears. For example:

Date               c1    col2    col3    col4    col5    rl6    d7 
01/01/2016 00:00   37.04   36.57   35.77   37.56   36.79   35.90   38.15 

... and given an array with the column names ['col2','col3','col4','col5'],

I need to compare these names with header names, and wherever the column name matches the array, then get the corresponding value in a row. For example, col2 should give value 36.57

smci
  • 32,567
  • 20
  • 113
  • 146
anmol
  • 85
  • 1
  • 8
  • 1
    This is just a straight lookup with `df.loc[:, list_of_col_names].values`. What output do you want: a pandas Series, a list, a numpy array...? Appending `.values` transforms from a DataFrame to a numpy array. – smci Sep 13 '19 at 00:20
  • Thats right. I did the same thing. After the posting the question,I come up with this idea – anmol Sep 13 '19 at 04:21
  • anmol you stated things backwards: you don't want to to *get the column headers for every time that a specific value appears*; you want to *get the array of values given an input list of column names*. – smci Sep 15 '19 at 23:23

2 Answers2

0

Try this:

# Make a Series containing the columns you want to retrieve
s = pd.Series(['col1','col2','col3','col4','col5'])

# Get the list of valid column names
# Notice that you don't have a `col1` column in your sample input
valid = s[s.isin(df.columns)]

df[valid]

Result

    col2   col3   col4    col5
0  36.57  35.77  37.56  36z.79
Code Different
  • 90,614
  • 16
  • 144
  • 163
0

suppose your data frame is

df

and you have the array,

l = ['col2', 'col3', 'col4', 'col5']

Then you will get the corresponding values of labels by,

[df[label].values[0] for label in l if label in df]

Output of your example will be

[36.57, 35.77, 37.56, 36.79]

hope it helps you.