Я пытаюсь реорганизовать фрейм данных в соответствии с ответом на этот вопрос: сдвинуть определенные строки определенного столбца pandas фрейм данных . Тем не менее, когда я пытаюсь разблокировать фрейм данных, я получаю следующую ошибку: ValueError: Index contains duplicate entries, cannot reshape
Я не понимаю, каким образом во-первых появляются дублирующиеся значения после того, как я отменю стек, поскольку столбец индекса представляет собой последовательность увеличивающихся чисел. Кто-нибудь может объяснить, почему существуют дубликаты и как решить эту проблему?
#original dataframe
<class 'pandas.core.frame.DataFrame'>
Int64Index: 21374 entries, 145 to 25978
Data columns (total 47 columns):
id 21374 non-null int64
country_id 21374 non-null int64
league_id 21374 non-null int64
season 21374 non-null object
stage 21374 non-null int64
date 21374 non-null object
match_api_id 21374 non-null int64
home_team_api_id 21374 non-null int64
away_team_api_id 21374 non-null int64
home_team_goal 21374 non-null int64
away_team_goal 21374 non-null int64
goal 13325 non-null object
shoton 13325 non-null object
shotoff 13325 non-null object
foulcommit 13325 non-null object
card 13325 non-null object
cross 13325 non-null object
corner 13325 non-null object
possession 13325 non-null object
BSA 11856 non-null float64
Home Team 21374 non-null object
Away Team 21374 non-null object
League 21374 non-null object
Country 21374 non-null object
home_player_1 21374 non-null object
home_player_2 21374 non-null object
home_player_3 21374 non-null object
home_player_4 21374 non-null object
home_player_5 21374 non-null object
home_player_6 21374 non-null object
home_player_7 21374 non-null object
home_player_8 21374 non-null object
home_player_9 21374 non-null object
home_player_10 21374 non-null object
home_player_11 21374 non-null object
away_player_1 21374 non-null object
away_player_2 21374 non-null object
away_player_3 21374 non-null object
away_player_4 21374 non-null object
away_player_5 21374 non-null object
away_player_6 21374 non-null object
away_player_7 21374 non-null object
away_player_8 21374 non-null object
away_player_9 21374 non-null object
away_player_10 21374 non-null object
away_player_11 21374 non-null object
winner 21374 non-null object
dtypes: float64(1), int64(9), object(37)
memory usage: 7.8+ MB
Создание фрейма данных
#add column of ones as counter
columns = match.columns[match.columns.get_loc('home_player_1'):match.columns.get_loc('away_player_1')+1].values
columns = list(columns)
player_appearences = match.groupby(columns[0]).size().reset_index()
player_appearences.rename(columns = {0:"Count_{}".format(player_appearences.columns[0][len(player_appearences.columns[0])-1])}, inplace = True, errors='raise')
player_appearences
for i in range(1,2):
player_appearences2 = match.groupby(columns[i]).size().reset_index()
player_appearences2
player_appearences2.rename(columns = {0:"Count_{}".format(player_appearences2.columns[0][len(player_appearences2.columns[0])-1])}, inplace = True, errors='raise')
player_appearences = player_appearences.merge(right = player_appearences2,how="outer",left_on ="{}".format(player_appearences.columns[0]),right_on = "{}".format(player_appearences2.columns[0]))
player_appearences
#overwrite nans in first column with names in current [i] player column
player_appearences = player_appearences.rename(columns = {"home_player_2":"home_player_1","Count_2":"Count_1"}).stack().unstack()