stratofortress
stratofortress

Reputation: 453

pandas dataframe loc failing to identify correct name

First, I had to import some data by running the following:

from eliteprospect import eliteprospect_scraper as ep
import numpy as np
import pandas as pd

nhl_2020 = ep.getPlayers('nhl', '2019-20')
players = pd.concat([nhl_2020])
lakings = players.loc[players['team'] == 'Los Angeles Kings']
lakingsdefs = lakings.loc[lakings['fw_def'] == 'DEF']
lakingsdefs_pos = lakingsdefs[['playername','position']]
##print(lakingsdefs_pos)

Then, I end up having a very simple dataframe created with Pandas as follows:

                            playername position
                178      Drew Doughty         D
                295       Sean Walker         D
                382          Matt Roy         D
                403        Ben Hutton         D
                554  Kurtis MacDermid         D
                632       Joakim Ryan         D
                743    Mikey Anderson         D
                758        Paul LaDue         D
                859   Tobias Björnfot         D
                876       Kale Clague         D

I tried the following code to pick out 'Joakim Ryan.'

print(lakingsdefs_pos.loc[lakingsdefs_pos['playername'] == 'Joakim Ryan'])

Unfortunately, it failed to pick out the row with 'Joakim Ryan' in it.

On the other hand, when I tried the same thing with 'position,' it does seem to work.

print(lakingsdefs_pos.loc[lakingsdefs_pos['position'] == 'D'])

Just prints out the entire list, as it should.

I am hoping to output something like the following:

       playername position
632  Joakim Ryan         D

Upvotes: 1

Views: 34

Answers (1)

wwnde
wwnde

Reputation: 26676

Lets try .str.contains() method;

lakingsdefs_pos[lakingsdefs_pos['playername'].str.contains('Joakim Ryan')]#If you only have the two columns

lakingsdefs_pos.loc[lakingsdefs_pos['playername'].str.contains('Joakim Ryan'),['playername','position']]#If you have other columns other than position and playername


      playername   position
632  Joakim Ryan        D

Upvotes: 2

Related Questions