0

I have the following dataframe:

df = pd.DataFrame(np.random.randn(4, 1), index=['mark13', 'luisgimenez', 'miguel72', 'luis34'],columns=['probability'])


             probability
mark13         -1.054687
luisgimenez     0.081224
miguel72       -0.893619
luis34         -1.576941

I would like to remove the rows where the last character in the index string does not contain a number .

The desired output would look something like this : (dropping the row where the index does not finishes with a number)

             probability
mark13         -1.054687
miguel72       -0.893619
luis34         -1.576941

I am sure the direction I need to get is the boolean indexing but I do not know how could I reference the last character in the index name

2 Answers 2

1
#use isdigt to check last char of your index to be used as a mask array to filter rows.
df[[e[-1].isdigit() for e in df.index]]
Out[496]: 
          probability
mark13      -0.111338
miguel72     0.548725
luis34       0.682949
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the str accessor to check if the last character is a number:

df[df.index.str[-1].str.isdigit()]
Out: 
          probability
mark13      -0.350466
miguel72     1.220434
luis34      -0.962123

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.