I know Pandas isn't really built to use with for-loops, but I have a specific task I'll have to do many times and it'd really save a lot of time if I could abstract some of it away with a function that I can call.
A generic version of my dataframe looks like this:
df = pd.DataFrame({'Name': pd.Categorical(['John Doe', 'Jane Doe', 'Bob Smith']), 'Score1': np.arange(3), 'Score2': np.arange(3, 6, 1)})
Name Score1 Score2
0 John Doe 0 3
1 Jane Doe 1 4
2 Bob Smith 2 5
What I want to do is take the method:
df.loc[df.Name == 'Jane Doe', 'Score2']
Which should return 4, but iterate through it with a for-loop like so:
def pull_score(people, score):
for i in people:
print df.loc[df.Name == people[i], score]
So if I wanted to I could call:
the_names = ['John Doe', 'Jane Doe', 'Bob Smith']
pull_score(the_names, 'Score2')
And get:
3
4
5
The error message I currently get is:
TypeError: list indices must be integers, not str
I've looked at some of the other answers relating to this error message and Pandas such as this one: Python and JSON - TypeError list indices must be integers not str and this one: How to solve TypeError: list indices must be integers, not list?
But didn't see the answer in either of them for what I'm trying to do and I don't believe iterrows() or itertuple() would apply since I need Pandas to find the values first.
