3

Suppose having a simple dataframe like the following:

data = {'grades Feb':[10,20,30,40,50], 'grades
Jan':[5,10,15,20,25],'grades
April':[1,2,3,4,5],'months':['Feb','April','Jan','Feb','April']} 
df=pd.DataFrame(data) df.lookup(df.index,df.months)

I want the output of the code to be something like this

array([10,  2, 15, 40,  5], dtype=int64)

without changing the column names to be equivalent to the names that are in 'months' column or changing 'months' column names, I just want to extract row values that are in columns where there names are a subset of my "months" column values. How this could be done using regular exprestion or a dataframe function. PS: that's a simple example to clarify my problem that is bigger than that where I couldn't simply change column names.

cs95
  • 330,695
  • 80
  • 606
  • 657
Chaymae Ahmed
  • 301
  • 4
  • 12

2 Answers2

3

Deprecation Notice: lookup was deprecated in v1.2.0

Using lookup and recreate your column's keys

df.lookup(df.index,'grades '+ df.months)
Out[1070]: array([10,  2, 15, 40,  5], dtype=int64)
Henry Ecker
  • 31,792
  • 14
  • 29
  • 50
BENY
  • 296,997
  • 19
  • 147
  • 204
2

Deprecation Notice: lookup was deprecated in v1.2.0

Do this with df.lookup:

v = df.filter(like='grades').rename(columns=lambda x: x.split()[1])
print(v)
   Feb  Jan  April
0   10    5      1
1   20   10      2
2   30   15      3
3   40   20      4
4   50   25      5

grades = v.lookup(df.index, df.months)

<! ->

print(grades)
array([10,  2, 15, 40,  5])
Henry Ecker
  • 31,792
  • 14
  • 29
  • 50
cs95
  • 330,695
  • 80
  • 606
  • 657