I have a table with cities (the pandas.DataFrame object with a single 'city' column) table and the pandas.Series - cities object, which is the first column of this table ( cities = table['city'] ).

I need to sort the cities in such a way that only the words that satisfy this condition are left in it. In this case, I need to leave only cities in cities beginning with the letter 'ΠΌ' . I need something similar to the action described below, but only with the help of pandas, not lists:

 lst = [word for word in cities if word[0] == 'ΠΌ'] 

How can I accomplish this? I tried it, but it gives me a KeyError: False

 lst = table.loc[cities[0] == 'ΠΌ'] 

Although, if you need to leave only the fields with the specified value, it works:

 lst= table.loc[cities != 'ΠΌΠ°Π³Π°Π΄Π°Π½'] 

How is it possible in the pandas.Series object to leave only the fields starting with a given letter?

    1 answer 1

    Source DataFrame:

     In [250]: table Out[250]: city 0 ΠΌΠ°Π³Π°Π΄Π°Π½ 1 КиСв 2 ΠœΠ°Ρ€ΠΈΡƒΠΏΠΎΠ»ΡŒ 3 Π—Π°ΠΏΠΎΡ€ΠΎΠΆΡŒΠ΅ 4 Москва 

    Solution options:

     In [252]: table[table['city'].str.lower().str.startswith('ΠΌ')] Out[252]: city 0 ΠΌΠ°Π³Π°Π΄Π°Π½ 2 ΠœΠ°Ρ€ΠΈΡƒΠΏΠΎΠ»ΡŒ 4 Москва 

    or using regular expressions:

     In [253]: table[table['city'].str.contains('^ΠΌ', flags=re.I)] Out[253]: city 0 ΠΌΠ°Π³Π°Π΄Π°Π½ 2 ΠœΠ°Ρ€ΠΈΡƒΠΏΠΎΠ»ΡŒ 4 Москва In [254]: table[table['city'].str.match('^ΠΌ.*$', flags=re.I)] Out[254]: city 0 ΠΌΠ°Π³Π°Π΄Π°Π½ 2 ΠœΠ°Ρ€ΠΈΡƒΠΏΠΎΠ»ΡŒ 4 Москва 
    • Well done. Thank you - fedotsoldier