If there is a list of words to check…
JavaScript
x
2
1
word_list = ['word1', 'word2', 'word3']
2
and a data frame like
JavaScript
1
5
1
Word,Score_a,Score_b,Score_c
2
word5,10,15,20
3
word6,40,60,80
4
word3,40,20,10
5
What is the fastest way to find the corresponding scores for each word in the given word list?
For example, 40, 20, 10
for 'word3'
.
Advertisement
Answer
To elaborate on comments above:
JavaScript
1
3
1
df.set_index('Word') #set Word column as index
2
df.loc['word5', :] # access row
3
Output:
JavaScript
1
4
1
Score_a 10
2
Score_b 15
3
Score_c 20
4
If you don’t want to set Word as index, you can also use .iloc
:
JavaScript
1
2
1
df.iloc[0, :]
2
Output:
JavaScript
1
5
1
Word word5
2
Score_a 10
3
Score_b 15
4
Score_c 20
5
Note the main difference between the two (other than the fact that you can specify column name with loc
) is that loc
is inclusive of the last term when you specify a range.