如何使用 pandas 在 python 中按字母对行值进行排序

How to sort row values in by letter in python using pandas

所以我有一个 excel 文件,其中包含一些初创公司的信息,我想显示城市位置以“s”或“p”开头的初创公司的详细信息。如何使用 pandas?

执行此操作

数据:

City  Location
Pune
Mumbai
Mumbai
Hyderabad
Burnsville
Bengaluru
Mumbai
Menlo Park
Bengaluru
Gurgaon
New Delhi
Bengaluru
Mumbai
Noida
Bengaluru

这不起作用:

 df[df['City Location'].str.startswith(('s','p'))]

使用Series.str.startswith with tuple if need test column City Location and filter by boolean indexing:

编辑:如果需要测试大写值:

df[df['City Location'].str.startswith(('S','P'), na=False)]

如果需要测试小写或大写:

df[df['City Location'].str.lower().str.startswith(('s','p'), na=False)]