[英]Python - First line reading CSV not in order
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:当读取包含单列游戏列表的 CSV 文件时,第一行/顶行中的游戏显示乱序,如下所示:
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:
添加 'index_col=0' 列出每个游戏,如下所示:
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 :)谢谢 :)
Need to add " names= " parameter when you read the CSV file.读取 CSV 文件时需要添加“名称= ”参数。
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.也可以在数据帧上使用value_counts()来查找值的频率。
(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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.