I have the following dataframe:
>>> d = {'route1': ['a', 'b'], 'route2': ['c', 'd'], 'val1': [1,2]}
>>> df = pd.DataFrame(data=d)
>>> df
route1 route2 val1
0 a c 1
1 b d 2
What I am trying to do is to pass a list that contains some column names and print the row value associated with that columns:
>>> def row_eval(row, list):
>>> print(row.loc[list])
In the dataframe above, I first find all the columns that contains the name "route" and then apply the row_val func to each row. However I get the following err:
>>> route_cols = [col for col in df.columns if 'route' in col]
>>> route_cols
['route1', 'route2']
>>> df.apply(lambda row: row_eval(row, route_cols)
KeyError: "None of [Index(['route1', 'route2'], dtype='object')] are in the [index]"
Result should look like this:
route1 a
route2 c
route1 b
route2 d