繁体   English   中英

计算熊猫循环中的缺失值

[英]Counting missing values in a Pandas loop

  • 如何在Pandas中运行循环,该循环返回包含零值的缺失值的列表?

循环应使用full_scores_list中的值,检查该值是否在Home_team_scores列中。 如果这样,则计算并输出频率,否则返回零。

我的名单很长,在两个赛季中一支球队的得分不同。

       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

大致情况如何:

com_vals = df['Home_team_scores'].unique()
df['full_scores_list'].apply(lambda v: v in com_vals)

根据评论改进:

除了lambda函数,您可以对apply()使用辅助函数:

com_vals = df['Home_team_scores'].unique()
def helper():
  return v in com_vals
df['full_scores_list'].apply(helper)

您可以使用helper()函数对输出和条件进行更细粒度的控制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM