import numpy as np
import pandas as pd
pd.Series(data=None, index=None, dtype=None, name=None, copy=False)
ar_list = [3,10,3,4,5]
print(type(ar_list))
s1 = pd.Series(ar_list)
print(s1)
print(type(s1))
#
#0 3
#1 10
#2 3
#3 4
#4 5
#dtype: int64
#
np_rand = np.arange(1,6)
s1 = pd.Series(np_rand)
s1
#0 1
#1 2
#2 3
#3 4
#4 5
#dtype: int32
s1.index
#RangeIndex(start=0, stop=5, step=1)
list(s1.index)
#[0, 1, 2, 3, 4]
print(s1.values, type(s1.values))
#[1 2 3 4 5]
s1[1]
#2
s1[2] = 50
s1
#0 1
#1 2
#2 50
#3 4
#4 5
#dtype: int32
s1[-1] = 20
s1
# 0 1
# 1 2
# 2 50
# 3 4
# 4 5
#-1 20
#dtype: int64
s1[-1] = 20
print(s1)
print(s1.index)
# 0 1
# 1 2
# 2 50
# 3 4
# 4 5
#-1 20
#dtype: int64
#Int64Index([0, 1, 2, 3, 4, -1], dtype='int64')
s1["a"] = 40
s1.index
#Index([0, 1, 2, 3, 4, -1, 'a'], dtype='object')
d = {'a': 1, 'b': 2, 'c': 3}
ser = pd.Series(data=d)
ser
#a 1
#b 2
#c 3
#dtype: int64
ser.index
ser.values
#Index(['a', 'b', 'c'], dtype='object')
#array([1, 2, 3], dtype=int64)
ser['a']
#1
ser["s"] = 50
#a 1
#b 2
#c 3
#s 50
#dtype: int64
ser[0]
#1
ser[-1]
#50
d = {'a': 1, 5: 2, 'c': 3}
ser1 = pd.Series(data=d)
ser1
#a 1
#5 2
#c 3
#dtype: int64
s = pd.Series(100,index=range(5))
s
#0 100
#1 100
#2 100
#3 100
#4 100
#dtype: int64
data = np.array(['a','b','c','d'])
s = pd.Series(data,index=[100,101,102,103])
s
#100 a
#101 b
#102 c
#103 d
#dtype: object
(2) 从指定索引的字典构造序列。
d = {'a': 1, 'b': 2, 'c': 3}
ser = pd.Series(d, index=['a', 'b', 'c'])
ser
#a 1
#b 2
#c 3
#dtype: int64
d = {'a': 1, 'b': 2, 'c': 3}
ser = pd.Series(data=d, index=['x', 'b', 'z'])
ser
#x NaN
#b 2.0
#z NaN
#dtype: float64
d = {'a': 1, 'b': 2, 'c': 3}
ser = pd.Series(data=d, index=['c', 'b', 'a'])
ser
#c 3
#b 2
#a 1
#dtype: int64
dict_data1 = {
"Beijing":2200,
"Shanghai":2500,
"Shenzhen":1700
}
data1 = pd.Series(dict_data1)
data1.name = "City_Data"
data1.index.name = "City_Name"
data1
#City_Name
#Beijing 2200
#Shanghai 2500
#Shenzhen 1700
#Name: City_Data, dtype: int64
np_rand = np.arange(1,6)
s1 = pd.Series(np_rand)
s1
#0 1
#1 2
#2 3
#3 4
#4 5
#dtype: int32
s1[1] = 50
print("s1:",s1)
print("np_rand:",np_rand)
#s1: 0 1
#1 50
#2 3
#3 4
#4 5
#dtype: int32
#np_rand: [ 1 50 3 4 5]
my_list = [1,2,3,4,5,6]
s2 = pd.Series(my_list)
s2
#0 1
#1 2
#2 3
#3 4
#4 5
#5 6
#dtype: int64
s2[1] = 50
print("s2:",s2)
print("my_list:",my_list)
#s2: 0 1
#1 50
#2 3
#3 4
#4 5
#5 6
#dtype: int64
#my_list: [1, 2, 3, 4, 5, 6]
s = pd.Series(np.random.rand(5))
print(s)
print(s[3], type(s[3]), s[3].dtype)
#0 0.777657
#1 0.622071
#2 0.348129
#3 0.756216
#4 0.287849
#dtype: float64
#0.7562162366628223 float64
s = pd.Series(np.random.rand(5),index=list("abcde"))
print(s["b"], type(s["b"]), s["b"].dtype)
#0.26319645172526607 float64
s = pd.Series([6,7,8,9,10],index = ['a','b','c','d','e'])
print(s)
print(s[['a','c','d']])
#a 6
#b 7
#c 8
#d 9
#e 10
#dtype: int64
#a 6
#c 8
#d 9
#dtype: int64
s1 = s[["b","a","e"]]
s1["b"] = 10
print("s1:",s1)
print("s源数据:",s)
#s1: b 10
#a 6
#e 10
#dtype: int64
#s源数据: a 6
#b 7
#c 8
#d 9
#e 10
#dtype: int64
s = pd.Series(np.random.rand(10))
s
#0 0.927452
#1 0.235768
#2 0.516178
#3 0.277643
#4 0.697771
#5 0.273533
#6 0.133503
#7 0.185826
#8 0.687192
#9 0.316528
#dtype: float64
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print(s)
print(s[1:4])
#a 1
#b 2
#c 3
#d 4
#e 5
#dtype: int64
#b 2
#c 3
#d 4
#dtype: int64
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print(s[-3:])
#c 3
#d 4
#e 5
#dtype: int64
s1= pd.Series([6,7,8,9,10],index = ['a','b','c','d','e'])
s1["b":"d"]
#b 7
#c 8
#d 9
#dtype: int64
s1= pd.Series([6,7,8,9,10],index = ['e','d','a','b','a'])
s1
#c 8
#dtype: int64
s = pd.Series(np.random.rand(15))
s
#0 0.819404
#1 0.552555
#2 0.792454
#3 0.215595
#4 0.824303
#5 0.970804
#6 0.997465
#7 0.519955
#8 0.354990
#9 0.758266
#dtype: float64
print(s.head())
print(s.head(1))
#0 0.819404
#1 0.552555
#2 0.792454
#3 0.215595
#4 0.824303
#dtype: float64
#0 0.819404
#dtype: float64
print(s.tail())
#5 0.970804
#6 0.997465
#7 0.519955
#8 0.354990
#9 0.758266
#dtype: float64
s = pd.Series(np.random.rand(5),index=list("abcde"))
s1 = s.reindex(list("cde"))
print("============s1=========")
print(s1)
print("============s=========")
print(s)
#============s1=========
#c 0.525886
#d 0.859566
#e 0.767330
#dtype: float64
#============s=========
#a 0.148972
#b 0.934014
#c 0.525886
#d 0.859566
#e 0.767330
#dtype: float64
s1 = pd.Series(np.random.rand(3), index=["Kelly","Anne","T-C"])
s2 = pd.Series(np.random.rand(3), index=["Anne","Kelly","LiLy"])
print("==========s1=========")
print(s1)
print("==========s2=========")
print(s2)
print("==========s1+s2=========")
print(s1+s2)
#==========s1=========
#Kelly 0.481159
#Anne 0.066326
#T-C 0.916705
#dtype: float64
#==========s2=========
#Anne 0.090194
#Kelly 0.150472
#LiLy 0.220991
#dtype: float64
#==========s1+s2=========
#Anne 0.156520
#Kelly 0.631632
#LiLy NaN
#T-C NaN
#dtype: float64
s = pd.Series(np.random.rand(5),index=list("abcde"))
s1 = s.drop("a")
print(s1)
print(s)
#b 0.918685
#c 0.613762
#d 0.142165
#e 0.309032
#dtype: float64
#a 0.630504
#b 0.918685
#c 0.613762
#d 0.142165
#e 0.309032
#dtype: float64
s = pd.Series(np.random.rand(5),index=list("abcde"))
s1 = s.drop("a",inplace=True)
print(s1)
print(s)
#None
#b 0.946778
#c 0.733088
#d 0.793721
#e 0.681853
#dtype: float64
s1 = pd.Series(np.random.rand(5),index=list("abcde"))
print(s1)
s1["s"] = 100
print(s1)
#a 0.743596
#b 0.778193
#c 0.036640
#d 0.324620
#e 0.282358
#dtype: float64
#a 0.743596
#b 0.778193
#c 0.036640
#d 0.324620
#e 0.282358
#s 100.000000
#dtype: float64