湘潭学校网站建设 精诚磐石网络购物网网站建设开题报告

当前位置: 首页 > news >正文

湘潭学校网站建设 精诚磐石网络,购物网网站建设开题报告,免费空间官网,合肥大建设1.创建DataFrame对象 概述 DataFrame是一个表格型的结构化数据结构#xff0c;它含有一组或多组有序的列#xff08;Series#xff09;#xff0c;每列可以是不同的值类型#xff08;数值、字符串、布尔值等#xff09;。 DataFrame是Pandas中的最基本的数据结构对象结构化数据结构它含有一组或多组有序的列Series每列可以是不同的值类型数值、字符串、布尔值等。 DataFrame是Pandas中的最基本的数据结构对象简称df可以认为df就是一个二维数据表这个表有行有列有索引 DataFrame是Pandas中最基本的数据结构Series的许多属性和方法在DataFrame中也一样适用. 创建方式 字典方式创建 import pandas as pd ​ dict_data {id: [1, 2, 3],name: [张三, 李四, 王五],age: [18, 20, 22] }

使用默认自增索引

字典中的key值是df对象的列名,value值是对应列的数据值

df1 pd.DataFrame(datadict_data) print(df1) print(type(df1)) ​

通过index参数指定索引, columns参数指定列的位置

df2 pd.DataFrame(datadict_data, index[A, B, C], columns[id, age, name]) print(df2) 列表元组方式创建 list_data [(1, 张三, 18),(2, 李四, 20),(3, 王五, 22)] df3 pd.DataFrame(datalist_data,index[A, B, C],  # 手动指定索引columns[id, name, age])  # 手动指定列名 print(df3) ​

输出结果如下id  name  age

A   1  张三   18 B   2  李四   20 C   3  王五   22
2.Series的常用属性 常见属性 ​ 属性说明loc使用索引值取子集iloc使用索引位置取子集dtype或dtypesSeries内容的类型TSeries的转置矩阵shape数据的维数sizeSeries中元素的数量valuesSeries的值indexSeries的索引值 ​ 代码演示 # 加载数据 import pandas as pd ​

读取csv文件, 设置 id列为: 索引列

data pd.read_csv(data/nobel_prizes.csv, index_colid) data.head() # 默认值只展示前5行数据 loc属性 first_row data.loc[941] print(first_row)        # 获取第一行数据, 但是是以列的方式展示的 print(type(first_row))  # class pandas.core.series.Series iloc属性 first_row data.iloc[0] # 使用索引位置获取自己 print(first_row)        # 获取第一行数据, 但是是以列的方式展示的 print(type(first_row))  # class pandas.core.series.Series dtype 或者 dtypes print(first_row.dtype)          # 打印Series的元素类型, object表示字符串 print(first_row[year].dtype)  # 打印Series的year列的元素类型, int64 ​

打印Series的year列的元素类型, 该列值为字符串, 字符串没有dtype属性, 所以报错.

print(first_row[firstname].dtype)   ​ shape 和 size属性 print(first_row.shape) # 维度 ​

结果为: (7,) 因为有7列元素

​ ​ print(first_row.size) # 元素个数: 7 values 属性 print(first_row.values) # 获取Series的元素值 index属性 print(first_row.index)  # 获取Series的索引 ​ print(first_row.keys()) # Series对象的keys()方法, 效果同上. 3.Series的常用方法 常见方法 ​ 方法说明append连接两个或多个Seriescorr计算与另一个Series的相关系数cov计算与另一个Series的协方差describe计算常见统计量drop_duplicates返回去重之后的Seriesequals判断两个Series是否相同get_values获取Series的值作用与values属性相同hist绘制直方图isinSeries中是否包含某些值min返回最小值max返回最大值mean返回算术平均值median返回中位数mode返回众数quantile返回指定位置的分位数replace用指定值代替Series中的值sample返回Series的随机采样值sort_values对值进行排序to_frame把Series转换为DataFrameunique去重返回数组value_counts统计不同值数量keys获取索引值head查看前5个值tail查看后5个值 ​ 代码演示 import pandas as pd ​

创建s对象

s1 pd.Series(data[1, 2, 3, 4, 2, 3], index[A, B, C, D, E, F])

查看s对象值数量

print(len(s1))

查看s对象前5个值, n默认等于5

print(s1.head()) print(s1.head(n2))

查看s对象后5个值, n默认等于5

print(s1.tail()) print(s1.tail(n2))

获取s对象的索引

print(s1.keys())

s对象转换成python列表

print(s1.tolist()) print(s1.to_list())

s对象转换成df对象

print(s1.to_frame())

s对象中数据的基础统计信息

print(s1.describe())

s对象最大值、最小值、平均值、求和值…

print(s1.max()) print(s1.min()) print(s1.mean()) print(s1.sum())

s对象数据值去重, 返回s对象

print(s1.drop_duplicates())

s对象数据值去重, 返回数组

print(s1.unique())

s对象数据值排序, 默认升序

print(s1.sort_values(ascendingTrue))

s对象索引值排序, 默认升序

print(s1.sort_index(ascendingFalse))

s对象不同值的数量, 类似于分组计数操作

print(s1.value_counts()) 小案例: 电影数据 # 加载电影数据 movie pd.read_csv(data/movie.csv)   movie.head() ​

获取 导演名(列)

director movie.director_name      # 导演名 director movie[director_name]   # 导演名, 效果同上 director ​

获取 主演在脸书的点赞数(列)

actor_1_fb_likes movie.actor_1_facebook_likes # 主演在脸书的点赞数 actor_1_fb_likes.head() ​

统计相关

director.value_counts() # 不同导演的 电影数 director.count()        # 统计非空值(即: 有导演名的电影, 共有多少), 4814 director.shape          # 总数(包括null值), (4916,) ​

查看详情

actor_1_fb_likes.describe() # 显示主演在脸书点击量的详细信息: 总数,平均值,方差等… director.describe()         # 因为是字符串, 只显示部分统计信息
4.Series的布尔索引 从scientists.csv数据集中列出大于Age列的平均值的具体值具体步骤如下 加载并观察数据集 import pandas as pd ​ df pd.read_csv(data/scientists.csv) print(df)

print(df.head())

输出结果如下Name        Born        Died  Age          Occupation

0     Rosaline Franklin  1920-07-25  1958-04-16   37             Chemist 1        William Gosset  1876-06-13  1937-10-16   61        Statistician 2  Florence Nightingale  1820-05-12  1910-08-13   90               Nurse 3           Marie Curie  1867-11-07  1934-07-04   66             Chemist 4         Rachel Carson  1907-05-27  1964-04-14   56           Biologist 5             John Snow  1813-03-15  1858-06-16   45           Physician 6           Alan Turing  1912-06-23  1954-06-07   41  Computer Scientist 7          Johann Gauss  1777-04-30  1855-02-23   77       Mathematicia ​

演示下, 如何通过布尔值获取元素.

bool_values [False, True, True, False, False, False, True, False] df[bool_values] ​

输出结果如下Name        Born        Died  Age          Occupation

1        William Gosset  1876-06-13  1937-10-16   61        Statistician 2  Florence Nightingale  1820-05-12  1910-08-13   90               Nurse 6           Alan Turing  1912-06-23  1954-06-07   41  Computer Scientist ​ 计算Age列的平均值 # 获取一列数据 df[列名] ages df[Age] print(ages) print(type(ages)) print(ages.mean()) ​

输出结果如下

0    37 1    61 2    90 3    66 4    56 5    45 6    41 7    77 Name: Age, dtype: int64 class pandas.core.series.Series 59.125 输出大于Age列的平均值的具体值 print(ages[ages ages.mean()]) ​

输出结果如下

1    61 2    90 3    66 7    77 Name: Age, dtype: int64 总结 # 上述格式, 可以用一行代码搞定, 具体如下 df[ages avg_age]                  # 筛选(活的)年龄 大于 平均年龄的科学家信息 df[df[Age] df.Age.mean()]       # 合并版写法.
5.Series的运算 Series和数值型变量计算时变量会与Series中的每个元素逐一进行计算 两个Series之间计算时索引值相同的元素之间会进行计算索引值不同的元素的计算结果会用NaN值(缺失值)填充。 Series和数值型变量计算 # 加法 print(ages 10)

乘法

print(ages * 2) ​

输出结果如下

0     47 1     71 2    100 3     76 4     66 5     55 6     51 7     87 Name: Age, dtype: int64 0     74 1    122 2    180 3    132 4    112 5     90 6     82 7    154 Name: Age, dtype: int64 两个Series之间计算时索引值相同的元素之间会进行计算索引值不同的元素的计算结果会用NaN值(缺失值)填充 print(ages ages) print( * 20) print(pd.Series([1, 100])) print( * 20) print(ages pd.Series([1, 100])) ​

输出结果如下

0     74 1    122 2    180 3    132 4    112 5     90 6     82 7    154 Name: Age, dtype: int640      1 1    100 dtype: int640     38.0 1    161.0 2      NaN 3      NaN 4      NaN 5      NaN 6      NaN 7      NaN dtype: float64 6.DataFrame常用属性和方法 基础演示 import pandas as pd ​

加载数据集, 得到df对象

df pd.read_csv(data/scientists.csv) ​ print( 常用属性 )

查看维度, 返回元组类型 - (行数, 列数), 元素个数代表维度数

print(df.shape)

查看数据值个数, 行数*列数, NaN值也算

print(df.size)

查看数据值, 返回numpy的ndarray类型

print(df.values)

查看维度数

print(df.ndim)

返回列名和列数据类型

print(df.dtypes)

查看索引值, 返回索引值对象

print(df.index)

查看列名, 返回列名对象

print(df.columns) print( 常用方法 )

查看前5行数据

print(df.head())

查看后5行数据

print(df.tail())

查看df的基本信息

df.info()

查看df对象中所有数值列的描述统计信息

print(df.describe())

查看df对象中所有非数值列的描述统计信息

exclude:不包含指定类型列

print(df.describe(exclude[int, float]))

查看df对象中所有列的描述统计信息

include:包含指定类型列, all代表所有类型

print(df.describe(includeall))

查看df的行数

print(len(df))

查看df各列的最小值

print(df.min())

查看df各列的非空值个数

print(df.count())

查看df数值列的平均值

print(df.mean()) DataFrame的布尔索引 # 小案例, 同上, 主演脸书点赞量 主演脸书平均点赞量的 movie[movie[actor_1_facebook_likes] movie[actor_1_facebook_likes].mean()] ​

df也支持索引操作

movie.head()[[True, True, False, True, False]] DataFrame的计算 scientists * 2 # 每个元素, 分别和数值运算 scientists scientists # 根据索引进行对应运算 scientists scientists[:4] # 根据索引进行对应运算, 索引不匹配, 返回NAN

  1. DataFrame-索引操作 Pandas中99%关于DF和Series调整的API, 都会默认在副本上进行修改, 调用修改的方法后, 会把这个副本返回 这类API都有一个共同的参数: inplace, 默认值是False 如果把inplace的值改为True, 就会直接修改原来的数据, 此时这个方法就没有返回值了 通过 set_index()函数 设置行索引名字 # 读取文件, 不指定索引, Pandas会自动加上从0开始的索引 movie pd.read_csv(data/movie.csv)   movie.head() ​

    设置 电影名 为索引列.  

    movie1 movie.set_index(movie_title) movie1.head() ​

    如果加上 inplaceTrue, 则会修改原始的df对象

    movie.set_index(movie_title, inplaceTrue)   movie.head()    # 原始的数据并没有发生改变. 加载数据的时候, 直接指定索引列 通过reset_index()函数, 可以重置索引 # 加上inplace, 就是直接修改 源数据. movie.reset_index(inplaceTrue) movie.head()
    8.DataFrame-修改行列索引 方式1: rename()函数, 可以对原有的行索引名 和 列名进行修改 movie pd.read_csv(data/movie.csv, index_colmovie_title) movie.index[:5]  # 前5个行索引名 ​ movie.columns[:5]  # 前5个列名 ​

    手动修改下 行索引名 和 列名

    idx_rename {Avatar: 阿凡达, Pirates of the Caribbean: At Worlds End: 加勒比海盗} col_rename {color: 颜色, director_name: 导演名} ​

    通过rename()函数, 对原有的行索引名 和 列名进行修改

    movie.rename(indexidx_rename, columnscol_rename).head() 方式2:把 index 和 columns属性提取出来, 修改之后, 再赋值回去 index类型不能直接修改,需要先将其转成列表, 修改列表元素, 再整体替换 movie pd.read_csv(data/movie.csv, index_colmovie_title) ​

    提取出 行索引名 和 列名, 并转成列表.

    index_list movie.index.tolist() columns_list movie.columns.tolist() ​

    修改列表元素值

    index_list[0] 阿凡达 index_list[1] 加勒比海盗 ​ columns_list[0] 颜色 columns_list[1] 导演名 ​

    重新把修改后的值, 设置成 行索引 和 列名

    movie.index index_list movie.columns columns_list ​

    查看数据

    movie.head(5) 9.添加-删除-插入列 添加列 movie pd.read_csv(data/movie.csv) ​

    通过 df[列名] 值 的方式, 可以给df对象新增一列, 默认: 在df对象的最后添加一列.

    movie[has_seen] 0   # 新增一列, 表示: 是否看过(该电影)   ​

    新增一列, 表示: 导演和演员 脸书总点赞数

    movieactor_director_facebook_likes ​ movie.head()    # 查看内容 删除列 或者 行 # movie.drop(has_seen) # 报错, 需要指定方式, 按行删, 还是按列删.

    movie.drop(has_seen, axiscolumns)   # 按列删

    movie.drop(has_seen, axis1)           # 按列删, 这里的1表示: 列

    ​ movie.head().drop([0, 1])                # 按行索引删, 即: 删除索引为0和1的行 插入列 有点特殊, 没有inplace参数, 默认就是在原始df对象上做插入的. # insert() 表示插入列.   参数解释: loc:插入位置(从索引0开始计数), column列名, value值

    总利润 总收入 - 总预算

    movie.insert(loc1, columnprofit, valuemovie[gross] - movie[budget]) movie.head()