3.1. Series Create¶
3.1.1. From Python sequence¶
list
tuple
set
frozenset
import pandas as pd
import numpy as np
pd.Series([1, 2, 3, 4])
# 0 1
# 1 2
# 2 3
# 3 4
# dtype: int64
pd.Series([1., 2., 3., 4.])
# 0 1.0
# 1 2.0
# 2 3.0
# 3 4.0
# dtype: float64
pd.Series([1, 2, None, 4])
# 0 1.0
# 1 2.0
# 2 NaN
# 3 4.0
# dtype: float64
pd.Series(['a', 'b', 'c', 'd'])
# 0 a
# 1 b
# 2 c
# 3 d
# dtype: object
import pandas as pd
list('abcd')
# ['a', 'b', 'c', 'd']
pd.Series(list('abcd'))
# 0 a
# 1 b
# 2 c
# 3 d
# dtype: object
3.1.2. From Python range¶
import pandas as pd
pd.Series(range(4))
# 0 0
# 1 1
# 2 2
# 3 3
# dtype: int64
3.1.3. From Numpy ndarray
¶
import pandas as pd
import numpy as np
pd.Series(np.arange(4.0))
# 0 0.0
# 1 1.0
# 2 2.0
# 3 3.0
# dtype: float64
3.1.4. From Date Range¶
From
pd.Timestamp
From
pd.date_range()
More information in Date and Time Types
import pandas as pd
pd.Series(pd.date_range(start='1969-07-16', end='1969-07-24'))
# 0 1969-07-16
# 1 1969-07-17
# 2 1969-07-18
# 3 1969-07-19
# 4 1969-07-20
# 5 1969-07-21
# 6 1969-07-22
# 7 1969-07-23
# 8 1969-07-24
# dtype: datetime64[ns]
3.1.5. Length¶
import pandas as pd
s = pd.Series([1, 2, 3, 4])
len(s)
# 9
3.1.6. Assignments¶
"""
* Assignment: Series Create Float
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min
English:
1. Create `result: pd.Series` with 5 float numbers
2. One of those values must be `None`
Polish:
1. Stwórz `result: pd.Series` z 5 liczbami zmiennoprzecinkowymi
2. Jedną z tych wartości musi być `None`
Tests:
>>> type(result) is pd.Series
True
>>> result
0 1.1
1 2.2
2 NaN
3 4.4
4 5.5
dtype: float64
"""
# Given
import pandas as pd
result = ...
"""
* Assignment: Series Create Randint
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min
English:
1. Set random seed to zero
2. Create `result: pd.Series` with 10 random digits (`int` from `0` to `9`)
Polish:
1. Ustaw ziarno losowości na zero
2. Stwórz `result: pd.Series` z 10 losowymi cyframi (`int` from `0` to `9`)
Tests:
>>> type(result) is pd.Series
True
>>> result
0 5
1 0
2 3
3 3
4 7
5 9
6 3
7 5
8 2
9 4
dtype: int64
"""
# Given
import numpy as np
import pandas as pd
np.random.seed(0)
result = ...
"""
* Assignment: Series Create Even
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min
English:
1. Create `result: pd.Series` with 10 even numbers
Polish:
1. Stwórz `result: pd.Series` z 10 liczbami parzystymi
Tests:
>>> type(result) is pd.Series
True
>>> result
0 0
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
dtype: int64
"""
# Given
import pandas as pd
import numpy as np
np.random.seed(0)
"""
* Assignment: Series Create Dates
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min
English:
1. Gagarin flown to space on 1961-04-12
2. Armstrong set foot on the Moon on 1969-07-21
3. Create `result: pd.Series` with days between Gagarin's launch and Armstrong's first step
4. How many days passed?
Polish:
1. Gagarin poleciał w kosmos w 1961-04-12
2. Armstrong postawił stopę na Księżycu w 1969-07-21
3. Stwórz `result: pd.Series` z dniami pomiędzy startem Gagarina a pierwszym krokiem Armstronga
4. Jak wiele dni upłynęło?
Tests:
>>> type(result) is pd.Series
True
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.max_columns', 10)
>>> pd.set_option('display.max_rows', 10)
>>> result # doctest: +NORMALIZE_WHITESPACE
0 1961-04-12
1 1961-04-13
2 1961-04-14
3 1961-04-15
4 1961-04-16
...
3018 1969-07-17
3019 1969-07-18
3020 1969-07-19
3021 1969-07-20
3022 1969-07-21
Length: 3023, dtype: datetime64[ns]
"""
# Given
import pandas as pd