import pandas as pd
# 生成数组
print(pd.Series([1, 2, 3, 4]))
print("1,------------")
# 查看索引信息
print(pd.Series([1, 2, 3, 4]).index)
print("2,----------")
# 有定义的输出
print(pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']))
print("3,--------")
# 输出多个索引
print(pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])[['a', 'c']])
print("4,-----")
# 更改值
a = pd.Series([1, 2, 3, 4], index=['a', 'c', 'c', 'd'])
a['d'] = 5
print(a)
print("5,--------")
# 增加值
a['e'] = 6
print(a)
print("6,---------")
# 添加字典键值对
print(pd.Series({"qinlu": "handsome", "lulu": "smart"}))
print("7,--------")
# 转换成二维框架
print(pd.DataFrame({"name": ["qinlu", "lulu", "qinqin"], "sex": ["male", "male", "female"], "age": [18, 18, 20]}))
print("8,--------")
# 改变行标签
print(pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6]], columns=list("abcd")))
print("9,-------")
# 改变列标签
print(pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6]], index=list("ab"), columns=list("abcd")))
print("10,-------")
# 查看数据类型
b = {"name": ["qinlu", "lulu", "qinqin"], "sex": ["male", "male", "female"], "age": [18, 18, 20]}
df = pd.DataFrame(b)
print(df.info())
print("11,--------")
# 切片
print(df["age"])
print(df.age)
print("12,-----")
# 更改某列元素
df['age'] = 22
print(df)
print("13,---------")
# 更改某列某行元素
# df['age'][0] = 23
print(df)
print("14,-------")
# 筛选年龄为18的数组
c = {"name": ["qinlu", "lulu", "qinqin"], "sex": ["male", "male", "female"], "age": [18, 18, 20]}
df = pd.DataFrame(c)
print(df.age == 18)
print("15,----------")
# 多逻辑判断
print(df.query("(age == 18) & (sex == 'male')"))
print("16,--------")
# 加1
df.age = df.age + 1
print(df.age)
print("17,-----")
# 大于
print(df.age > 19)
print("18,---------")
# 翻转
print(~ df.age > 19)
print("19,-------")
# 小于等于19的排除掉
print(df[~ (df.age > 19)])
print("20,-------")
# 等于19的排除掉
print(df[~ (df.age == 19)])
print("21,-------")
# 高阶函数iloc针对行切
print(df.iloc[1])
print(df[0:1])
print("22,------")
# 高阶函数loc针对标签切
df.index = list("abc")
print(df)
print("23,------")
print(df.loc["a"])
print("24,------")