import pandas as pd
from pandas import Series
obj = Series([3,9,12])
obj
0 3
1 9
2 12
dtype: int64
Series is similar to an array in Numpy, but you can use index to name the data.
obj.values
array([ 3, 9, 12])
obj.index
RangeIndex(start=0, stop=3, step=1)
ww2_cas = Series([8700000, 4300000, 3000000, 2100000, 400000], index=['USSR', 'Germany', 'China', 'Japan', 'USA'])
ww2_cas
USSR 8700000
Germany 4300000
China 3000000
Japan 2100000
USA 400000
dtype: int64
You can also access individual data using an index
ww2_cas['USA']
400000
ww2_cas>4000000
USSR True
Germany True
China False
Japan False
USA False
dtype: bool
ww2_cas[ww2_cas>4000000]
USSR 8700000
Germany 4300000
dtype: int64
'USSR' in ww2_cas
True
ww2_dic = ww2_cas.to_dict()
ww2_dic
{'China': 3000000,
'Germany': 4300000,
'Japan': 2100000,
'USA': 400000,
'USSR': 8700000}
ww2_Series = Series(ww2_dic)
ww2_Series
China 3000000
Germany 4300000
Japan 2100000
USA 400000
USSR 8700000
dtype: int64
In [ ]:
countries = ['China', 'Germony', 'Japan', 'USA', 'USSR', 'Argentina']
obj2 = Series(ww2_dic, index=countries)
obj2
China 3000000.0
Germony NaN
Japan 2100000.0
USA 400000.0
USSR 8700000.0
Argentina NaN
dtype: float64
pd.isnull(obj2)
China False
Germony True
Japan False
USA False
USSR False
Argentina True
dtype: bool
pd.notnull(obj2)
China True
Germony False
Japan True
USA True
USSR True
Argentina False
dtype: bool
Series can be named as a whole
obj2.name = 'World War II casualties'
obj2
China 3000000.0
Germony NaN
Japan 2100000.0
USA 400000.0
USSR 8700000.0
Argentina NaN
Name:World War II casualties, dtype: float64
Recommended Posts