Oduvan
Oduvan

Reputation: 2757

I have string index in pandas DataFrame how can I select by startswith?

In dataframe I have an index from column Nation.

But I can't do

df[df.Nation.str.startswith('U')]

without reseting index.

How can I get str object of index?

Upvotes: 22

Views: 64520

Answers (2)

andnik
andnik

Reputation: 2804

You can also just set index in DataFrame constructor if you don't want to have separate name for your index column:

df = pd.DataFrame({'A':[2,3,5], 'Z':[4,5,6]}, index=['Uw','A', 'Ur'])
print(df)
    A  Z
Uw  2  4
A   3  5
Ur  5  6

All operations described in another answer by @jezrail work in the same way.

print(df[df.index.str.startswith('U')])
    A  Z
Uw  2  4
Ur  5  6

Upvotes: 0

jezrael
jezrael

Reputation: 863226

Use index which works with str nice:

df[df.index.str.startswith('U')]

Sample:

df = pd.DataFrame({'Nation':['Uw','A', 'Ur'],
                   'A':[2,3,5],
                   'Z':[4,5,6]})


df = df.set_index(['Nation'])
print (df)
        A  Z
Nation      
Uw      2  4
A       3  5
Ur      5  6

print (df[df.index.str.startswith('U')])
        A  Z
Nation      
Uw      2  4
Ur      5  6

If need select by level of MultiIndex use get_level_values:

df = df.set_index(['Nation', 'A'])
print (df)
          Z
Nation A   
Uw     2  4
A      3  5
Ur     5  6

print (df[df.index.get_level_values('Nation').str.startswith('U')])
          Z
Nation A   
Uw     2  4
Ur     5  6

Upvotes: 38

Related Questions