import pandas as pd
# 读取全部文件内容
print(pd.read_csv(r"D:\mycode\用pandas\data\dataAnalyst_sql.csv", encoding="gbk", names=list("abcdefg")))
print("1,----------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106004712647-650264061.png)
# 查看数据类型
print(pd.read_csv(r"D:\mycode\用pandas\data\dataAnalyst_sql.csv", encoding="gbk").info())
print("2,------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106004726305-386446055.png)
# 默认切前5行
print(pd.read_csv(r"D:\mycode\用pandas\data\dataAnalyst_sql.csv", encoding="gbk").head())
print("3,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106004741816-628394549.png)
# 默认切后5行
print(pd.read_csv(r"D:\mycode\用pandas\data\dataAnalyst_sql.csv", encoding="gbk").tail())
print("4,---------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106004752146-968795333.png)
# 改变字段的数据类型为字符串
a = pd.read_csv(r"D:\mycode\用pandas\data\dataAnalyst_sql.csv", encoding="gbk")
a.top = a.top.astype("str")
print(a.info())
print("5,----------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106004805156-507066514.png)
# 改变字段的数据类型为数字
b = pd.read_csv(r"D:\mycode\用pandas\data\dataAnalyst_sql.csv", encoding="gbk")
# 把空值排除掉
b = b[~b['top'].isnull()]
b.top = b.top.astype('int64')
print(b.info())
print("6,----------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106004823010-1281738696.png)
print(b[["bottom", 'top']])
print("7,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106004838875-370836040.png)
# 求平均值
b["avg_2"] = (b.bottom + b.top)/2
print(b["avg_2"])
print("8,---------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106004856560-730952667.png)
# 平均值大于15的所以城市
c = b.query("avg > 15").city
print(c)
print("9,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106004910566-1014399337.png)
# 平均值大于15的城市等于上海
d = b[(b.city == '上海') & (b.avg > 15)]
print(d)
print("10,------------------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106004925488-210961688.png)
# 转置:列转行、行转列
print(b.T)
print("11,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106004939363-759432414.png)
# 升序
print(b.sort_values(by='avg'))
print(b.avg.sort_values())
print("12,----------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005015111-1808541966.png)
# 返回数据框
print(b.sort_values("avg"))
print("13,------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005029596-1340201914.png)
# 降序
print(b.sort_values("avg", ascending=False))
print("14,---------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005044404-2144556182.png)
# 多个条件降序
print(b.sort_values(["avg", 'city'], ascending=False))
print("15,---------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005059540-1563228056.png)
# 将平均值排名默认
b["rank"] = b.avg.rank(ascending=False, method="first")
# 将平均值排名最小
b["rank"] = b.avg.rank(ascending=False, method="min")
# 将平均值排名最大
b["rank"] = b.avg.rank(ascending=False, method="max")
print(b)
print("16,---------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005120204-1623016324.png)
b.sort_values(by="avg", ascending=False)
print(b)
print("17,----------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005135049-1271496519.png)
# 查唯一值
print(b.workYear.unique())
print("18,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005148210-52528684.png)
# 查唯一值计数
print(b.workYear.value_counts())
print("19,-----")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005159945-499276639.png)
# 描述统计汇总
print(b.describe())
print("20,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005212569-136320184.png)
# 求平均值的描述信息
print(b.avg.describe())
print("21,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005229827-933385746.png)
# 累加
print(b.sum())
print("22,------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005253181-1532654159.png)
# 中位数
print(b.median())
print("23,----------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005309328-499383677.png)
# 标准差
print(b.std())
print("24,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005423196-1370803685.png)
# 方差
print(b.var())
print("25,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005437658-283152381.png)
# 数据框累加
print(b.cumsum())
print("26,---------------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005455074-1779302365.png)
# 平均值累加
print(b.avg.cumsum())
print("27,-----------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005509085-204789517.png)
# 切割5等份
e = pd.cut(b.avg, bins=5)
print(e)
print("28,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005523530-1188780058.png)
# 切割4等份挂挡
e = pd.cut(b.avg, bins=4, labels=list("abcd"))
print(e)
print("29,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005538088-744730517.png)
# 指定区间等份
print(pd.cut(b.avg, bins=[0, 5, 10, 20, 30]))
print("30,------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005555247-382679396.png)
# 按城市分组计数
print(b.groupby(by="city").count())
print("31,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005608096-850276228.png)
# 聚合最大值
print(b.groupby(by="city").max())
print(b.groupby(by="city")["avg"].max())
print("32,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005627639-1128715201.png)
# 聚合最小值
print(b.groupby(by="city").min())
print(b.groupby(by="city")["avg"].min())
print("33,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005651087-160065022.png)
# 多重索引聚合
print(b.groupby(by=["city", "workYear"]).min())
print("34,--------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005711252-1747097695.png)
# 不同城市间最大值和最小值的差值
for k, v in b.groupby(by=["city"]):
print("最大值是:", max(v.avg), "最小值是:", min(v["avg"]))
print("差值是:", max(v.avg) - min(v["avg"]))
print("35,-------")
![](https://img2020.cnblogs.com/blog/1452370/202101/1452370-20210106005731236-502652063.png)