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
| import pandas as pd import numpy as np
dict = { "k1":45, "k2":25, "k3":np.nan, "k4":17, } s = pd.Series(dict, name="price", dtype="float64")
print(f"s.loc['k2']: {s.loc['k2']}, 类型: {type(s.loc['k2'])}") print(f"s.loc['k4']: {s.loc['k4']}, 类型: {type(s.loc['k4'])}")
s2 = s.loc[['k2','k4']] print(f"\ns2 类型: {type(s2)}, 数据: \n{s2}")
s3 = s.loc['k2':'k4'] print(f"\ns3 类型: {type(s3)}, 数据: \n{s3}")
bool_mask1 = s > 20 s4 = s.loc[ bool_mask1 ] print(f"\nbool_mask1 类型: {type(bool_mask1)}, 数据: \n{bool_mask1}") print(f"\ns4 类型: {type(s4)}, 数据: \n{s4}")
bool_mask2 = [True, False, True, False] s5 = s.loc[ bool_mask2 ] print(f"\nbool_mask2 类型: {type(bool_mask2)}, 数据: \n{bool_mask2}") print(f"\ns5 类型: {type(s5)}, 数据: \n{s5}")
|