0
df.loc[df['column_name'] == some_value]

selects data from a row or rows in a dataframe given the specified column.

What is the equivalent if I need to specify the 'row name' ?

0

1 Answer 1

1

If I understand correctly, you can pass the index directly to loc:

Example:

df

       a   b
group       
abc    6   9
abc    3  12
abc    5  56
def    8  32
gfd    4  65
uio    2  87
def    1  31
poi    8   2
fgb    3   3

df.loc['abc']
       a   b
group       
abc    6   9
abc    3  12
abc    5  56
Sign up to request clarification or add additional context in comments.

4 Comments

Using your example: df.loc['abc'] does works as you describe but df.loc[df['abc'] == 6] does not. It gets "KeyError: 'abc'" and df.loc['a'] gets "KeyError: 'the label [a] is not in the [index]". The original question using these terms would be e.g df.loc[df['abc'] == 6] to extract all rows which contained 6 (the 1st 'abc' row in your example
@Alasdair the issue you are encountering relates to whether or not the value is in the index (I assumed this is what you meant by 'row name'). If not, you would need to specify the column name. If I wanted all rows in column a containing the integer 3, I would slice: df.loc[df['a'] == 3].
and if I wanted all columns in row abc that has the integer 3 how would I slice it? This is my original question
@Alasdair "All columns in row abc" ... can you provide an actual example? I don't understand what you're looking for. Provide starting dataframe and the exact output you're looking for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.