I am trying to select the rows of data frame df_A whose index values ends with 1 or 4 and capture them in another data frame df_s1s4.
I am given the following hint: "pass a boolean function, which checks if an index string ends with 1 or 4, to .loc or .iloc methods."
I tried the following but couldn't get it to work.
import numpy as np
import pandas as pd
heights_A=pd.Series([176.2,158.4,167.6,156.2,161.4], index=['s1','s2','s3','s4','s5'])
weights_A=pd.Series([85.1,90.2,76.8,80.4,78.9], index=['s1','s2','s3','s4','s5'])
df_A=pd.DataFrame({'Student_height':heights_A, 'Student_weight':weights_A})
df_s1s4=df_A.loc[:,df_A.columns.str.endswith('1','4')]
print(df_s1s4)
Can anybody suggest how I might use a boolean function to solve this problem?