0

I want to list through items in column 2 named 'Teams'

I tried this..

for team in df['Teams']:

also tried

for team in df['Teams'].values:
ernest_k
  • 44,416
  • 5
  • 53
  • 99
john terry
  • 77
  • 8
  • what do you want to achieve after iterating? can you post a sample df? and an expected output? most pandas solutions are vectorized which doesnt require looping. you may also want to look at `iterrows()` , Thanks – anky Apr 13 '19 at 17:20
  • I'm trying to find specific teams from the list. – john terry Apr 13 '19 at 17:22
  • Providing an example of your problem and the result you are going to achieve helps you be clear and other users understand the problem. – sentence Apr 13 '19 at 17:24
  • you mean `df[df.Teams.isin(teamlist)]` ? if not i suggest you go through [this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and create an example and expected output. – anky Apr 13 '19 at 17:25

1 Answers1

2

Say your 'df' looks like :

>       Team   Score
    0   Cats      9
    1   Dogs      7

You can iterate through the column 'Team' by doing:

for i in df.get('Team'):
  print(i)

> Cats
  Dogs
Snehaa Ganesan
  • 509
  • 3
  • 10
  • but this doesnt require loop? `df['Team']` will give you this, let me know if I am misunderstanding. :) Thanks – anky Apr 13 '19 at 17:34
  • 1
    @anky_91 They wanted to iterate over the values, so based on the behavior they're looking for I gave an answer. But yes you can definitely get a list of values by simply doing df['Team'] – Snehaa Ganesan Apr 13 '19 at 17:38
  • 1
    Okay, got it. :) I am still stuck at the first question(why iterate) But that answer has to be answered by the OP. :) – anky Apr 13 '19 at 17:39
  • Sorry, that original code worked. There was an syntax error. I was trying to match teams in team col to another list. I don't know how to post code. I'm new here:) – john terry Apr 13 '19 at 17:43