[Python] How to use Pandas Series

Preparation

import pandas as pd
from pandas import Series

What is 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.

Get list of values
obj.values
array([ 3,  9, 12])
Get key list
obj.index
RangeIndex(start=0, stop=3, step=1)
Key update
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
From Series to dictionary type
ww2_dic = ww2_cas.to_dict()
ww2_dic
{'China': 3000000,
 'Germany': 4300000,
 'Japan': 2100000,
 'USA': 400000,
 'USSR': 8700000}
From dictionary type to Series
ww2_Series = Series(ww2_dic)
ww2_Series
China      3000000
Germany    4300000
Japan      2100000
USA         400000
USSR       8700000
dtype: int64
In [ ]:

Whether there is null
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
Not null
pd.notnull(obj2)

China         True
Germony      False
Japan         True
USA           True
USSR          True
Argentina    False
dtype: bool

Series name

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

[Python] How to use Pandas Series
How to use Pandas 2
[Python] Summary of how to use pandas
[Python] How to use list 1
How to use Python argparse
How to use Pandas Rolling
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to use Python Image Library in python3 series
Python: How to use async with
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] Let's use pandas
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
I tried to summarize how to use pandas in python
[Introduction to Python] Let's use pandas
How to use python zip function
[Introduction to Python] Let's use pandas
[Python] How to use Typetalk API
[Introduction to Python] How to use class in Python?
[Python] Use pandas to extract △△ that maximizes ○○
[Pandas] What is set_option [How to use]
[python] How to use __command__, function explanation
[Python] How to use import sys sys.argv
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use "deque" for Python data
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
Adding Series to columns in python pandas
How to use is and == in Python
[Blender x Python] How to use modifiers
How to use pandas Timestamp and date_range
[Question] How to use plot_surface of python
How to use xml.etree.ElementTree
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header