[英]Counting missing values in a Pandas loop
循環應使用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.