0

When reading a CSV with a list games in a single column, the game in the first/top row is displayed out of order, like so:

                        Fatal Labyrinth™
0                            Beat Hazard
1                             Dino D-Day
2                 Anomaly: Warzone Earth
3                        Project Zomboid
4           Avernum: Escape From the Pit

..with the code being:

my_data = pd.read_csv(r'C:\Users\kosta\list.csv', encoding='utf-16', delimiter='=')
print(my_data)

Fatal Labyrinth is, I suppose, not indexed. Adding 'index_col=0' lists each game, like so:

Empty DataFrame
Columns: []
Index: [Beat Hazard, Dino D-Day, more games etc...]

But this does not help, as the endgame here is to count each game and determine the most common, but when doing:

counts = Counter(my_data)
dictTime = dict(counts.most_common(3))
for key in dictTime:
    print(key)

..all I'm getting back is:

Fatal Labyrinth™

Thank you :)

2
  • I think you want to add argumet header=None in read_csv Commented Oct 10, 2020 at 10:33
  • Thanks Erfan. That does help a bit. The same list now starts with 0 and counting only shows the 0. Commented Oct 10, 2020 at 10:49

1 Answer 1

1

Need to add "names=" parameter when you read the CSV file.

my_data = pd.read_csv('test.csv',  delimiter='=', names=['Game_Name'])  # Game_Name is given as column name
print(my_data)
                      Game_Name
0              Fatal Labyrinth™
1                   Beat Hazard
2                    Dino D-Day
3        Anomaly: Warzone Earth
4               Project Zomboid
5  Avernum: Escape From the Pit

Also value_counts() can be used on the dataframe to find the frequency of the value.

(my_data.Game_Name.value_counts(ascending=False)).head(3)  # Top three most frequent value
Project Zomboid           1
Anomaly: Warzone Earth    1
Beat Hazard               1
Name: Game_Name, dtype: int64

In case, you need to get the top game name by its frequency,

(my_data.Game_Name.value_counts()).head(1).index[0]
'Project Zomboid'
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this helps. Is there a way to not list the corresponding number of times the game appears? i.e. the in your comment, 'Project Zomboid' appears once, but can I get rid of the 1?
You can use "head(1).index[0]" to the command. Added to the main answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.