# Shantanu's Blog

Corporate Consultant

## generate numpy array using list of lists

Here is the list of medals won by each country as on 7 th day of the game.

usa = ['USA', 30, 29, 28]
uk = ['UK', '20', '19', '14']
china = ['china', 19, 17, 20]

# list of lists of each country
gold_silver_bronze = [usa, uk, china]

# create numpy array
import numpy as np
olympic = np.array(gold_silver_bronze)

same as

# olympic = np.array([['USA', 30, 29, 28], ['UK', '20', '19', '14'], ['china', 19, 17, 20]])

# countries participated
olympic[:,0]

# total of gold
sum(np.array(olympic[:,1], np.int))

# welcome pandas
import pandas as pd

>>> df=pd.DataFrame(olympic)
>>> df
0   1   2   3
0    USA  30  29  28
1     UK  20  19  14
2  china  19  17  20

>>> df.dtypes
country    object
gold       object
silver     object
bronze     object
dtype: object

>>> df.gold=pd.to_numeric(df.gold)

>>> df.dtypes
country    object
gold        int64
silver     object
bronze     object
dtype: object

# which one is easier?

>>> df.gold.sum()
69
>>> sum(np.array(olympic[:,1], np.int))
69

# but how will you find the total of gold medals won by all countries?

>>> x = 0
>>> for i in gold_silver_bronze:
...     y = i[1]
...     x += int(y)
...
>>> x
69
_____

# total of medals won by USA
>>> usa[1:]
[30, 29, 28]
>>> sum(usa[1:])
87

>>> df['total']=df['gold']+df['silver']+df['bronze']
>>> df[['country', 'total']]
country  total
0     USA     87
1      UK     53
2   china     56

Labels: ,