The loop should take the values in the full_scores_list, check whether that value is in the Home_team_scores column. If so calculate and output the frequency, otherwise return zero.
I have a long list that has different scores for a team for two league seasons.
My code is:
all_scores = []
for score in range(0,len(full_scores_list)):
if full_scores_list[score] == '0 0':
all_scores.append(data1.Home_team_scores.value_counts()['0 0'])
elif full_scores_list[score] == '0 1':
all_scores.append(data1.Home_team_scores.value_counts()['0 1'])
elif full_scores_list[score] == '0 2':
all_scores.append(data1.Home_team_scores.value_counts()['0 2'])
all_scores.append(data1.Home_team_scores.value_counts()['0 2'])
elif full_scores_list[score] == '0 3':
all_scores.append(data1.Home_team_scores.value_counts()['0 3'])
elif full_scores_list[score] == '0 4':
all_scores.append(data1.Home_team_scores.value_counts()['0 4'])
elif full_scores_list[score] == '0 5':
all_scores.append(data1.Home_team_scores.value_counts()['0 5'])
if full_scores_list[score] == '1 0':
all_scores.append(data1.Home_team_scores.value_counts()['1 0'])
elif full_scores_list[score] == '1 1':
all_scores.append(data1.Home_team_scores.value_counts()['1 1'])
elif full_scores_list[score] == '1 2':
all_scores.append(data1.Home_team_scores.value_counts()['1 2'])
elif full_scores_list[score] == '1 3':
all_scores.append(data1.Home_team_scores.value_counts()['1 3'])
elif full_scores_list[score] == '1 4':
all_scores.append(data1.Home_team_scores.value_counts()['1 4'])
elif full_scores_list[score] == '1 5':
all_scores.append(data1.Home_team_scores.value_counts()['1 5'])
all_scores
How about something along the lines:
com_vals = df['Home_team_scores'].unique()
df['full_scores_list'].apply(lambda v: v in com_vals)
Improved according to comments:
Instead of the lambda-function, you can use a helper function for the apply()
:
com_vals = df['Home_team_scores'].unique()
def helper():
return v in com_vals
df['full_scores_list'].apply(helper)
You can have more fine-grained control on the outputs and conditions with the helper()
function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.