0

I have a DataFrame with a column of player NAMES and a column of player unique IDs. There may be more than one player with the same name (i.e. John Williams), but two unique player IDs (i.e. williamsjo01 & williamsjo02). When I create a dictionary of the two columns, where ever there is a key with multiple values, it only captures the latter value.

I am looking for a way for the keys with multiple values to be a list with multiple values. What I am thinking right now is possibly using a conditional statement such as:

if df['fullName'].value_counts() > 1:
    (creates list and appends multiple values to one key)
else:
    dict(zip(df['fullName'], df['playerID']

Appreciate the help!

vu2
  • 11
  • 1

1 Answers1

-1

Here is the solution for the problem.

import pandas as pd

df = pd.DataFrame({"NAME":['A','A','B','B','C'],
                   "ID":[1, 2, 1, 2,1]})

temp1 = df['NAME'].unique()
lis1 = temp1.tolist()
print(lis1)

temp2 = df['NAME'].value_counts()
lis2 = temp2.to_list()
print(lis2)


d = dict(zip(lis1, lis2))
for key, value in d.items():
  if value > 1:
      lis3 = df.loc[df['NAME'] == key, 'ID'].unique().tolist()
      d[key] = lis3

print(d)

Output:

{'A': [1, 2], 'B': [1, 2], 'C': 1}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Usfa
  • 16
  • 1