首页 > 解决方案 > Python Pandas: Filter rows based on position letter in string

问题描述

I'm pretty new to python and I'm still learning Pandas. I've been playing around and trying to learn things with Pandas that I wouldn't be able to do with excel ordinaril using it's filter function.

One thing that I have been trying to do is to filter based on the second letter in the column I choose to filter through.

For example, I have the following table:

Name HP Type

Pikachu 10 Electric

Charizard 200 Fire

Squirtle 300 Water

I want to create a filter that looks through the name column and if the second letter is 'h' then it gives me the data for charizard row.

In other words, it filters data by row based on the second letter specified in the column selected, in this case the column is "Name" and the letter was 'h'

I've tried to make this code by using a for loop and and index function but after trying for quite some time, I was unable to properly get it done and I wanted some feedback as to how I would go about appraoching this.

My code looks like the following:

import pandas as pd
df = pd.read_excel (r'C:\Users\Name\Desktop\pokemon.xlsx')
for column in (df['Name']):
    if column.index[1]  == 'h':
print (df [['Name', 'HP', 'Type']])

I'm new to this, so any tips on how to fix my code would be appreciated. Thanks.

标签: pythonpython-3.xpandassortingfilter

解决方案


IIUC,然后str.containsregex比赛一起使用。

df[df.Name.str.contains(r'^\wh\w*')]

印刷:

        Name   HP  Type
1  Charizard  200  Fire

推荐阅读