我对pandas有特别的问题:我需要在dataframe中选择以特定字母开头的行。详细信息:我已经将我的数据导入到dataframe中,并选择了我需要的列。我还将其范围缩小到我需要的行索引。现在我还需要在其他列中选择对象以字母'pl‘开头的行。
有没有办法只根据前两个字符来选择行?
我在想
pl = df[‘Code’] == pl*
但是由于行索引的原因,它不会工作。感谢您的建议!
发布于 2020-06-16 05:31:27
如果您在Series上使用string方法,则应返回true/false结果。然后,您可以将其用作过滤器,并结合.loc来创建数据子集。
new_df = df.loc[df[‘Code’].str.startswith('pl')].copy()
发布于 2020-06-16 05:18:05
使用startswith
执行以下操作:
df = df[df['Code'].str.startswith('pl')]
发布于 2020-06-16 05:21:08
条件只是一个筛选器,然后您需要将其应用于数据帧。作为过滤器,您可以使用方法Series.str.startswith
和do
df_pl = df[df['Code'].str.startswith('pl')]
https://stackoverflow.com/questions/62397170
复制相似问题