Python Forum
How to search for specific string in Pandas dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to search for specific string in Pandas dataframe
#1
Hi, Smile
I'm trying to extract lines from my dataframe using Pandas in a specific column named Equipe_Junior. For now I have ben able to extract my data when asking for the complete string for example: Quebec Remparts [QMJHL]. But I would like to go trough my dataframe for all [QMJHL] or [OHL] or any junior league so I can work stats with that, whithout having to ask for a specific junior team, just the league.

This is my code and results. Thanks for your help.

import pandas as pd
data= pd.read_csv(r'C:\Users\ben\PycharmProjects\draft2020\hockey_draft2012_click_test.csv')
df = pd.DataFrame(data, columns=['Ronde','Equipe','Nom','Equipe_Junior','MJ'])  # choose column from csv
df = df.fillna(0)  # replace nan with 0
select = df.loc[df['Equipe_Junior'] =='Quebec Remparts [QMJHL]']  # select players from that team only
print(select)
Output:
Result Ronde Equipe Nom Equipe_Junior MJ 11 1 Buffalo Mikhail Grigorenko Quebec Remparts [QMJHL] 217.0 123 5 Calgary Ryan Culkin Quebec Remparts [QMJHL] 0.0 165 6 Ottawa Francois Brassard Quebec Remparts [QMJHL] 0.0
Larz60+ write Nov-02-2020, 11:35 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time, please use bbcode tags on your future posts. Thank you.
Reply
#2
(Oct-22-2020, 07:19 PM)Coding_Jam Wrote: Hi, Smile
I'm trying to extract lines from my dataframe using Pandas in a specific column named Equipe_Junior. For now I have ben able to extract my data when asking for the complete string for example: Quebec Remparts [QMJHL]. But I would like to go trough my dataframe for all [QMJHL] or [OHL] or any junior league so I can work stats with that, whithout having to ask for a specific junior team, just the league.

This is my code and results. Thanks for your help.

import pandas as pd
data= pd.read_csv(r'C:\Users\ben\PycharmProjects\draft2020\hockey_draft2012_click_test.csv')
df = pd.DataFrame(data, columns=['Ronde','Equipe','Nom','Equipe_Junior','MJ']) # choose column from csv
df = df.fillna(0) # replace nan with 0
select = df.loc[df['Equipe_Junior'] =='Quebec Remparts [QMJHL]'] # select players from that team only
print(select)

Result
Ronde Equipe Nom Equipe_Junior MJ
11 1 Buffalo Mikhail Grigorenko Quebec Remparts [QMJHL] 217.0
123 5 Calgary Ryan Culkin Quebec Remparts [QMJHL] 0.0
165 6 Ottawa Francois Brassard Quebec Remparts [QMJHL] 0.0

Hey! Maybe you could use loc and str.contains? Something like this would select the rows containing either "QMJHL" or "OHL":

df.loc[df.loc[:, 'Equipe_Junior'].str.contains(r'(QMJHL|OHL)')]
In the code above, you would select the rwos containing either of the leagues because you create a boolean mask. Loc will select the rows in the dataframe based on this.

Hope it works!

Best,

E
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove specific values from dataframe jonah88888 0 337 Sep-24-2021, 05:09 AM
Last Post: jonah88888
  update values in one dataframe based on another dataframe - Pandas iliasb 2 1,480 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 783 Jun-22-2021, 07:57 PM
Last Post: snippsat
  Setting the x-axis to a specific column in a dataframe devansing 0 727 May-23-2021, 12:11 AM
Last Post: devansing
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 1,096 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Comparing results within a list and appending to pandas dataframe Aryagm 1 881 Dec-17-2020, 01:08 PM
Last Post: palladium
  merging dataframe into another on a specific row danlin123 0 618 Sep-23-2020, 02:42 PM
Last Post: danlin123
  PANDAS: DataFrame | White Spaces & Special Character Removal traibr 1 1,908 Sep-10-2020, 07:02 PM
Last Post: eddywinch82
  No Output In Pandas DataFrame Query eddywinch82 1 917 Aug-17-2020, 09:25 PM
Last Post: eddywinch82
  strange error from pandas dataframe djf123 1 2,235 Jul-27-2020, 05:25 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020