4

I have a Dataframe with 45 columns and 11k rows. This dataframe consist of players. Columns displaying their name, player_id, rating, height etc. Pretend you have the name of a player in the dataframe, or their ID, and you want to access the entire row of that player. You want to see all the information of that individual, but you only have one unique identifyer.

I tried using df.loc[[id_number]], but that only takes me to the index of the dataframe, which does not correspond to player_id.

Hopefully I explained it well enough. If you have any questions, please post them below.

Kenso33
  • 51
  • 6

4 Answers4

5
df.loc[df['column_name'] == some_value]

Related to https://stackoverflow.com/a/17071908/17487637

1

You can try applying a mask:

df[df.playerId == id_number]

Assuming playerId is the name of the column containing the player ids.

Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
  • 1
    Thank you very much, this worked perfectly. This taught me that mask's work other ways as well. Managed to create a new dataframe consisting only of players with a rating higher than '66'. I would upvote you, but apparently I cannot do that with only 11 points on my profile. – Kenso33 Dec 01 '21 at 10:43
  • 1
    @Kenso33 Yeah mask generalize to inequalities or functions, they also can be composed with boolean operators. Plenty of uses to that, they are the bread and butter of data filtering. – Learning is a mess Dec 01 '21 at 10:44
1

As far as I have understood, you want to query a dataframe based on one unique identifier. Let's suppose you only have player name.

df[df.player_name==playername]

Here playername is the variable where you will store your desired player name.

0

I use this in my code to find what i need in excel file. Mayby will be helpful for you.

 search = input('\t\t    Find row:  ')
 xlsxfile = pd.read_excel('your_filename.xlsx', engine='openpyxl')
df = xlsxfile[xlsxfile['name_of_your_column' ].str.contains(search , na=False)]

dx = (df[['name', 'player_id','height']])
print(dx) 

You will need some third party module for this like openpyxl.

Juru
  • 28
  • 3