Search code examples
pythonpandasdataframepandas-loc

Calling data frame values by index name


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')]

Solution

  • df.loc[df["index name"].str.startswith("5")]
    
        index name  a   b
    2   5-1         3   6
    3   5-2         4   8