0

I have this data-frame:

index
name    a     b
1-1     1     2
1-2     2     4
5-1     3     6
5-2     4     8
7-1     5     4
7-2     6     5

I want to call only values starting with 'index name' = 5

index
name     a     b
5-1      3     6
5-2      4     8

I tried:

df = df.loc['index name'] == 5 

But I get SyntaxError: invalid token

Any advise on how to do it? thanks!

EDIT

If 'index name' is a column, It works with:

df = df.loc[df['your column'] == 5]

However, if 'index name' is an index, it does not work.

EDIT 2

If 'index name' is index, it works with:

df.loc[df.index.str.startswith('5')]
0

1 Answer 1

2
df.loc[df["index name"].str.startswith("5")]

    index name  a   b
2   5-1         3   6
3   5-2         4   8
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, this one < df = df.loc[df['your column'] == 5] > worked if 'index name' is a column. However it does not work if 'index name' is an index.
with < df.loc[df["index name"].str.startswith("5")] > I still get < KeyError: index name >
@Jack_T if your column is an index, try: df.loc[df.index.str.startswith("5")]
Thanks it worked! but there is another issue when changing 'index_name'. Please have a look at edit 2, thanks!
@Jack_T, maybe try df.loc[df.index.str.startswith("5|7")] or df.loc[df.index.str.contains("^5|^7")]?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.