I have a df of users and the teams they belong. Another df with teams and their scores on different match. I want to select only the scores that match a user's team:
import pandas as pd
users = [['tom', 'team1'], ['tom', 'team2'], ['john', 'team2'], ['john', 'team3']]
match = [['team1', 100], ['team2', 120], ['team2', 80], ['team3', 70]]
users_df = pd.DataFrame(users, columns=['name', 'team'])
match_df = pd.DataFrame(match, columns=['team','score'])
user_teams = users_df['team'][users_df['name']== 'tom'].to_list()
scores = []
for x in user_teams:
a = match_df['score'][match_df['team'] == x].to_list()
scores.append(a)
print(scores)
So, the output should be team1 and team2 scores inside a list:
[100, 120, 80]
The problem is that with this code I get each item inside a list that appends to 'scores' list:
[[100], [120, 80]]
But if I run the code without to_list()
command on the line a = match_df['score'][match_df['team'] == x].to_list()
, I have the following as result:
[0 100
Name: score, dtype: object, 2 120
Name: score, dtype: object]
What am I doing wrong?
CodePudding user response:
there are 2 ways to achieve this:
- use
extend
to the list (notappend
) (as suggested by @Psidom) OR - flatten the scores such that:
[i for sg in scores for i in sg]
example:
scores = [[100], [120, 80]]
[i for sg in scores for i in sg] #sg for sub_group
desired result
[100, 120, 80]
CodePudding user response:
At the end of your group you can write:
out = [s for group in scores for s in group]
print(out)
This should remove the extra list within the list. Cheers