import pandas as pd
# 生成数组
print(pd.Series([1, 2, 3, 4]))
print("1,------------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170054731-456631048.png)
# 查看索引信息
print(pd.Series([1, 2, 3, 4]).index)
print("2,----------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170118819-1572327145.png)
# 有定义的输出
print(pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']))
print("3,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170132801-1110048397.png)
# 输出多个索引
print(pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])[['a', 'c']])
print("4,-----")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170152697-62522820.png)
# 更改值
a = pd.Series([1, 2, 3, 4], index=['a', 'c', 'c', 'd'])
a['d'] = 5
print(a)
print("5,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170205117-151873166.png)
# 增加值
a['e'] = 6
print(a)
print("6,---------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170217752-677502078.png)
# 添加字典键值对
print(pd.Series({"qinlu": "handsome", "lulu": "smart"}))
print("7,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170228940-1527703052.png)
# 转换成二维框架
print(pd.DataFrame({"name": ["qinlu", "lulu", "qinqin"], "sex": ["male", "male", "female"], "age": [18, 18, 20]}))
print("8,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170243004-1553093404.png)
# 改变行标签
print(pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6]], columns=list("abcd")))
print("9,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170253304-1038433338.png)
# 改变列标签
print(pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6]], index=list("ab"), columns=list("abcd")))
print("10,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170320900-401348515.png)
# 查看数据类型
b = {"name": ["qinlu", "lulu", "qinqin"], "sex": ["male", "male", "female"], "age": [18, 18, 20]}
df = pd.DataFrame(b)
print(df.info())
print("11,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170336079-1191900578.png)
# 切片
print(df["age"])
print(df.age)
print("12,-----")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170401420-716735808.png)
# 更改某列元素
df['age'] = 22
print(df)
print("13,---------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170413661-1075918957.png)
# 更改某列某行元素
# df['age'][0] = 23
print(df)
print("14,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170425351-709163545.png)
# 筛选年龄为18的数组
c = {"name": ["qinlu", "lulu", "qinqin"], "sex": ["male", "male", "female"], "age": [18, 18, 20]}
df = pd.DataFrame(c)
print(df.age == 18)
print("15,----------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170436939-2102369333.png)
# 多逻辑判断
print(df.query("(age == 18) & (sex == 'male')"))
print("16,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170458370-1232595042.png)
# 加1
df.age = df.age + 1
print(df.age)
print("17,-----")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170511438-62051475.png)
# 大于
print(df.age > 19)
print("18,---------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170527229-1770637330.png)
# 翻转
print(~ df.age > 19)
print("19,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170538934-1182876519.png)
# 小于等于19的排除掉
print(df[~ (df.age > 19)])
print("20,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170549273-1764780318.png)
# 等于19的排除掉
print(df[~ (df.age == 19)])
print("21,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170559708-56713810.png)
# 高阶函数iloc针对行切
print(df.iloc[1])
print(df[0:1])
print("22,------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170612642-1948767801.png)
# 高阶函数loc针对标签切
df.index = list("abc")
print(df)
print("23,------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170626026-1668597669.png)
print(df.loc["a"])
print("24,------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210103170637065-1164266114.png)