[英]Counting missing values in a Pandas loop
The loop should take the values in the full_scores_list, check whether that value is in the Home_team_scores column. 循环应使用full_scores_list中的值,检查该值是否在Home_team_scores列中。 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()
: 除了lambda函数,您可以对
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. 您可以使用
helper()
函数对输出和条件进行更细粒度的控制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.