-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpandas_Series.py
More file actions
64 lines (57 loc) · 1.62 KB
/
pandas_Series.py
File metadata and controls
64 lines (57 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
one of the most popular python library to manipulate and analyze data
"""
import pandas as pd
import numpy as np
# one of two important data, Series: one dimensional labeled array
# holding data types: str, int, float, python object etc.
# method 1: create from ndarray, if given index,
# index length should be as same as the array length
x = [_ for _ in range(100, 105, 1)]
sr1 = pd.Series(x, name="ID",
index=['a', 'b', 'c', 'd', 'e'])
print('-----sr1-----')
print(sr1)
# method 2: create from dict
d = {'a': 0, 'b': 1, 'c': 2, 'd': 3.0}
sr2 = pd.Series(d)
print('-----sr2-----')
print(sr2)
# data type changed
d = {'a': 0, 'b': 1, 'c': 'c', 'd': 3.0}
sr3 = pd.Series(d)
print('-----sr3-----')
print(sr3)
# NaN is standard missing data marker in pandas
# pay attention to the order
sr4 = pd.Series(d, index=['a', 'b', 'c', 'e', 'd'])
print('-----sr4-----')
print(sr4)
# from scalar value, index must be provided to tell the length
sr5 = pd.Series(2, index=['a', 'b', 'c'], name='scalar')
print('-----sr5-----')
print(sr5)
# ndarray-like and dict-like, it is mutable,
# and can be manipulated like dict and ndarray
# int type can not manipulate sqrt, exp !!!!
sr3['c'] = 2
print('-----changed sr3-----')
print(sr3)
print(sr3[1])
print(sr3+sr3)
print(sr3*3)
# print(np.sqrt(sr3)) raise AttributeError
print(sr3[sr3 >= 2])
print(sr3)
sr3['e'] = 4.0
sr3 = pd.Series(sr3, dtype=float, name='example')
print('---sr3 add item and name, change type---')
print(sr3)
print(np.sqrt(sr3))
print(sr3.name)
sr6 = sr3.rename("changed") # after pandas v 18
print('-----sr6-----')
print(sr6)
print(sr6.name)
print(sr3)
print(pd.__version__)