Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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

df = pd.DataFrame({'Nation':['Uw','A', 'Ur'],
                   'A':[2,3,5],
                   'Z':[4,5,6]})
df = df.set_index(['Nation'])
df[df.index.str.startswith('U')]
#If need select by level of MultiIndex use get_level_values:
df = df.set_index(['Nation', 'A'])
(
    df[df.index.get_level_values('Nation')
    .str.startswith('U')]
)


Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #I #string #index #pandas #DataFrame #I #select
ADD COMMENT
Topic
Name
8+2 =