1

I have a pandas dataframe of this kind:

data = {'Index':[1a,2a,3a,4a], 'col1':[20.1,20.2,20.3,20.4], 'col2': [30.2,30.5,30.7,30.5]}
df1 = pd.DataFrame(data)
df1 = df1.set_index('Index')
print(df1)

output:

        col1  col2
Index            
1a      20.1  30.2
2a      20.2  30.5
3a      20.3  30.7
4a      20.4  30.5

Now my aim is to select the row with a specific index, say 2a, so the output should be:

        col1  col2
Index            
2a      20.2  30.5

I tried it with following command (see e.g. this tutorial):

df1.loc[df1['Index'] == 2]

but for some reason it does not work, why?

2
  • just use loc? df1.loc[['2a']]? Commented Dec 21, 2021 at 19:10
  • 1
    If you want the row with index 18, which can have any other location in the df, just do df.iloc[df.index == 18] Commented Jan 28, 2024 at 17:45

1 Answer 1

2

Just use .loc:

>>> df.loc['2a']
col1    20.2
col2    30.5
Name: 2a, dtype: float64

>>> df.loc['2a', 'col1']
20.2
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.