我正在与numpy合作,并试图找出哪个平台在NA地区销售最多。
我有一个CSV文件保存了很多数据,如下所示:
Rank,Name,Platform,Year,Genre,Publisher,NA_Sales,EU_Sales,JP_Sales,Other_Sales,Global_Sales
1,Wii Sports,Wii,2006,Sports,Nintendo,41.49,29.02,3.77,8.46,82.74
2,Super Mario Bros.,NES,1985,Platform,Nintendo,29.08,3.58,6.81,0.77,40.24
3,Mario Kart Wii,Wii,2008,Racing,Nintendo,15.85,12.88,3.79,3.31,35.82
4,Wii Sports Resort,Wii,2009,Sports,Nintendo,15.75,11.01,3.28,2.96,33
5,Pokemon Red/Pokemon Blue,GB,1996,Role-Playing,Nintendo,11.27,8.89,10.22,1,31.37
6,Tetris,GB,1989,Puzzle,Nintendo,23.2,2.26,4.22,0.58,30.26
7,New Super Mario Bros.,DS,2006,Platform,Nintendo,11.38,9.23,6.5,2.9,30.01
8,Wii Play,Wii,2006,Misc,Nintendo,14.03,9.2,2.93,2.85,29.02
9,New Super Mario Bros. Wii,Wii,2009,Platform,Nintendo,14.59,7.06,4.7,2.26,28.62
10,Duck Hunt,NES,1984,Shooter,Nintendo,26.93,0.63,0.28,0.47,28.31
11,Nintendogs,DS,2005,Simulation,Nintendo,9.07,11,1.93,2.75,24.76我想打印在NA地区销售最多和销售量最多的平台。我该怎么做?
发布于 2017-03-04 23:49:42
对于熊猫,这是相当直截了当的。
代码:
# read csv data into a dataframe
df = pd.read_csv(data, skipinitialspace=True)
# roll up by NA Sales
platform_roll_up = df.groupby('Platform')['NA_Sales'].sum()
# find row with max sales
idx_max = platform_roll_up.idxmax()
# show platform and sales for max
print(idx_max, platform_roll_up[idx_max])结果:
Wii 101.71测试数据:
data = StringIO(u"""
Rank,Name,Platform,Year,Genre,Publisher,NA_Sales,EU_Sales,JP_Sales,Other_Sales,Global_Sales
1,Wii Sports,Wii,2006,Sports,Nintendo,41.49,29.02,3.77,8.46,82.74
2,Super Mario Bros.,NES,1985,Platform,Nintendo,29.08,3.58,6.81,0.77,40.24
3,Mario Kart Wii,Wii,2008,Racing,Nintendo,15.85,12.88,3.79,3.31,35.82
4,Wii Sports Resort,Wii,2009,Sports,Nintendo,15.75,11.01,3.28,2.96,33
5,Pokemon Red/Pokemon Blue,GB,1996,Role-Playing,Nintendo,11.27,8.89,10.22,1,31.37
6,Tetris,GB,1989,Puzzle,Nintendo,23.2,2.26,4.22,0.58,30.26
7,New Super Mario Bros.,DS,2006,Platform,Nintendo,11.38,9.23,6.5,2.9,30.01
8,Wii Play,Wii,2006,Misc,Nintendo,14.03,9.2,2.93,2.85,29.02
9,New Super Mario Bros. Wii,Wii,2009,Platform,Nintendo,14.59,7.06,4.7,2.26,28.62
10,Duck Hunt,NES,1984,Shooter,Nintendo,26.93,0.63,0.28,0.47,28.31
11,Nintendogs,DS,2005,Simulation,Nintendo,9.07,11,1.93,2.75,24.76
""")发布于 2017-03-05 00:40:21
用genfromtxt直接加载这个文件是直接的:
In [280]: data=np.genfromtxt('stack42602390.csv',delimiter=',',names=True, dtype=None)
In [281]: data
Out[281]:
array([ ( 1, b'Wii Sports', b'Wii', 2006, b'Sports', b'Nintendo', 41.49, 29.02, 3.77, 8.46, 82.74),
( 2, b'Super Mario Bros.', b'NES', 1985, b'Platform', b'Nintendo', 29.08, 3.58, 6.81, 0.77, 40.24),
( 3, b'Mario Kart Wii', b'Wii', 2008, b'Racing', b'Nintendo', 15.85, 12.88, 3.79, 3.31, 35.82),
....
(11, b'Nintendogs', b'DS', 2005, b'Simulation', b'Nintendo', 9.07, 11. , 1.93, 2.75, 24.76)],
dtype=[('Rank', '<i4'), ('Name', 'S25'), ('Platform', 'S3'), ('Year', '<i4'), ('Genre', 'S12'), ('Publisher', 'S8'), ('NA_Sales', '<f8'), ('EU_Sales', '<f8'), ('JP_Sales', '<f8'), ('Other_Sales', '<f8'), ('Global_Sales', '<f8')])b'string'只是显示字节串的Python3方式,这是genfromtxt的默认字符串格式。他们不会出现在Py2上。
结果是一个具有不同字段名和类型的结构化数组。它不是带有行和列的2d数组。
NA_Sales数据:
In [282]: data['NA_Sales']
Out[282]:
array([ 41.49, 29.08, 15.85, 15.75, 11.27, 23.2 , 11.38, 14.03,
14.59, 26.93, 9.07])其中最多的是:
In [283]: np.argmax(data['NA_Sales'])
Out[283]: 0以及相应的记录:
In [284]: data[0]
Out[284]: (1, b'Wii Sports', b'Wii', 2006, b'Sports', b'Nintendo', 41.49, 29.02, 3.77, 8.46, 82.74)为了最大限度地利用这个数组,您必须阅读结构化数组。
https://stackoverflow.com/questions/42602390
复制相似问题