一、Pandas如何对Categorical类型字段数据统计

实战场景:对Categorical类型字段数据统计,Categorical类型是Pandas拥有的一种特殊数据类型,这样的类型可以包含基于整数的类别展示和编码的数据

1.1主要知识点

  • 文件读写
  • 基础语法
  • Pandas
  • read_csv

实战:

1.2创建 python 文件

import pandas as pd
#读取csv文件
df = pd.read_csv("Telco-Customer-Churn.csv")
 
# 填充 TotalCharges 的缺失值
median = df["TotalCharges"][df["TotalCharges"] != ' '].median()
df.loc[df["TotalCharges"] == ' ', 'TotalCharges'] = median
df["TotalCharges"] = df["TotalCharges"].astype(float)
 
# 将分类列转换成 Categorical 类型
number_columns = ['tenure', 'MonthlyCharges', 'TotalCharges']
for column in number_columns:  df[column] = df[column].astype(float) #对三列变成float类型
for column in set(df.columns) - set(number_columns):  df[column] = pd.Categorical(df[column])
print(df.info())
print(df.describe(include=["category"]))

1.3运行结果

RangeIndex: 7043 entries, 0 to 7042  
Data columns (total 21 columns):
 #   Column            Non-Null Count  Dtype
---  ------            --------------  -----
 0   customerID        7043 non-null   category
 1   gender            7043 non-null   category
 2   SeniorCitizen     7043 non-null   category
 3   Partner           7043 non-null   category
 4   Dependents        7043 non-null   category
 5   tenure            7043 non-null   float64
 6   PhoneService      7043 non-null   category
 7   MultipleLines     7043 non-null   category
 8   InternetService   7043 non-null   category
 9   OnlineSecurity    7043 non-null   category
 10  OnlineBackup      7043 non-null   category
 11  DeviceProtection  7043 non-null   category
 12  TechSupport       7043 non-null   category
 13  StreamingTV       7043 non-null   category
 14  StreamingMovies   7043 non-null   category
 15  Contract          7043 non-null   category
 16  PaperlessBilling  7043 non-null   category
 17  PaymentMethod     7043 non-null   category
 18  MonthlyCharges    7043 non-null   float64
 19  TotalCharges      7043 non-null   float64
 20  Churn             7043 non-null   category
dtypes: category(18), float64(3)
memory usage: 611.1 KB
None
        customerID gender  SeniorCitizen Partner  ...        Contract PaperlessBilling     PaymentMethod Churn      
count         7043   7043           7043    7043  ...            7043             7043              7043  7043      
unique        7043      2              2       2  ...               3                2                 4     2      
top     0002-ORFBO   Male              0      No  ...  Month-to-month              Yes  Electronic check    No      
freq             1   3555           5901    3641  ...            3875             4171              2365  5174      

[4 rows x 18 columns] 

二、Pandas如何从股票数据找出收盘价最低行

实战场景:Pandas如何从股票数据找出收盘价最低行

2.1主要知识点

  • 文件读写
  • 基础语法
  • Pandas
  • read_csv

2.2创建 python 文件

"""
数据是CSV格式
1、加载到dataframe
2、找出收盘价最低的索引
3、根据索引找出数据行4 打印结果数据行
"""
import pandas as pd
 
df = pd.read_csv("./00700.HK.csv")
df["Date"] = pd.to_datetime(df["Date"])
df["Year"] = df["Date"].dt.year
df["Month"] = df["Date"].dt.month
print(df)
print(df.groupby("Year")["Close"].mean())
print(df.describe())

2.3运行结果

     Date     Open     High      Low    Close     Volume  Year  Month
0    2021-09-30  456.000  464.600  453.800  461.400   17335451  2021      9
1    2021-09-29  461.600  465.000  450.200  465.000   18250450  2021      9
2    2021-09-28  467.000  476.200  464.600  469.800   20947276  2021      9
3    2021-09-27  459.000  473.000  455.200  464.600   17966998  2021      9
4    2021-09-24  461.400  473.400  456.200  460.200   16656914  2021      9
...         ...      ...      ...      ...      ...        ...   ...    ...
4262 2004-06-23    4.050    4.450    4.025    4.425   55016000  2004      6
4263 2004-06-21    4.125    4.125    3.950    4.000   22817000  2004      6
4264 2004-06-18    4.200    4.250    3.950    4.025   36598000  2004      6
4265 2004-06-17    4.150    4.375    4.125    4.225   83801500  2004      6
4266 2004-06-16    4.375    4.625    4.075    4.150  439775000  2004      6

[4267 rows x 8 columns]
Year
2004      4.338686
2005      6.568927
2006     15.865951
2007     37.882724
2008     54.818367
2009     96.369679
2010    157.299598
2011    189.737398
2012    228.987045
2013    337.136066
2014    271.291498
2015    144.824291
2016    176.562041
2017    291.066667
2018    372.678862
2019    346.225203
2020    479.141129
2021    586.649189
Name: Close, dtype: float64

三、Pandas如何给股票数据新增年份和月份

实战场景:Pandas如何给股票数据新增年份和月份

3.1主要知识点

 

  • 文件读写
  • 基础语法
  • Pandas
  • Pandas的Series对象
  • DataFrame

实战:

3.2创建 python 文件

"""
给股票数据新增年份和月份
"""
import pandas as pd
 
df = pd.read_csv("./00100.csv")
print(df)
 
# to_datetime变成时间类型
df["Date"] = pd.to_datetime(df["Date"])
df["Year"] = df["Date"].dt.year
df["Month"] = df["Date"].dt.month
 
print(df)

3.3运行结果

            Date     Open     High      Low    Close     Volume
0     2021-09-30  456.000  464.600  453.800  461.400   17335451
1     2021-09-29  461.600  465.000  450.200  465.000   18250450
2     2021-09-28  467.000  476.200  464.600  469.800   20947276
3     2021-09-27  459.000  473.000  455.200  464.600   17966998
4     2021-09-24  461.400  473.400  456.200  460.200   16656914
...          ...      ...      ...      ...      ...        ...
4262  2004-06-23    4.050    4.450    4.025    4.425   55016000
4263  2004-06-21    4.125    4.125    3.950    4.000   22817000
4264  2004-06-18    4.200    4.250    3.950    4.025   36598000
4265  2004-06-17    4.150    4.375    4.125    4.225   83801500
4266  2004-06-16    4.375    4.625    4.075    4.150  439775000

[4267 rows x 6 columns]
           Date     Open     High      Low    Close     Volume  Year  Month
0    2021-09-30  456.000  464.600  453.800  461.400   17335451  2021      9
1    2021-09-29  461.600  465.000  450.200  465.000   18250450  2021      9
2    2021-09-28  467.000  476.200  464.600  469.800   20947276  2021      9
3    2021-09-27  459.000  473.000  455.200  464.600   17966998  2021      9
4    2021-09-24  461.400  473.400  456.200  460.200   16656914  2021      9
...         ...      ...      ...      ...      ...        ...   ...    ...
4262 2004-06-23    4.050    4.450    4.025    4.425   55016000  2004      6
4263 2004-06-21    4.125    4.125    3.950    4.000   22817000  2004      6
4264 2004-06-18    4.200    4.250    3.950    4.025   36598000  2004      6
4265 2004-06-17    4.150    4.375    4.125    4.225   83801500  2004      6
4266 2004-06-16    4.375    4.625    4.075    4.150  439775000  2004      6

[4267 rows x 8 columns]

四、Pandas如何获取表格的信息和基本数据统计

实战场景:Pandas如何获取表格的信息和基本数据统计

4.1主要知识点

  • 文件读写
  • 基础语法
  • Pandas
  • Pandas的Series对象
  • numpy

实战:

4.2创建 python 文件

import pandas as pd
import numpy as np
 
df = pd.DataFrame(  data={  "norm": np.random.normal(loc=0, scale=1, size=1000),  "uniform": np.random.uniform(low=0, high=1, size=1000),  "binomial": np.random.binomial(n=1, p=0.2, size=1000)},  index=pd.date_range(start='2021-01-01', periods=1000))
 
# df.info(),查看多少行,多少列,类型等基本信息
# df.describe(),查看每列的平均值、最小值、最大值、中位数等统计信息;
print(df.info())
print()
print(df.describe())

4.3运行结果  

DatetimeIndex: 1000 entries, 2021-01-01 to 2023-09-27
Freq: D
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype
---  ------    --------------  -----
 0   norm      1000 non-null   float64
 1   uniform   1000 non-null   float64
 2   binomial  1000 non-null   int32
dtypes: float64(2), int32(1)
memory usage: 27.3 KB
None

              norm      uniform     binomial
count  1000.000000  1000.000000  1000.000000
mean     -0.028664     0.496156     0.215000
std       0.987493     0.292747     0.411028
min      -3.110249     0.000629     0.000000
25%      -0.697858     0.238848     0.000000
50%      -0.023654     0.503438     0.000000
75%       0.652157     0.746672     0.000000
max       3.333271     0.997617     1.000000

五、Pandas如何使用日期和随机数生成表格数据类型

实战场景:Pandas如何使用日期和随机数生成表格数据类型

5.1主要知识点

  • 文件读写
  • 基础语法
  • Pandas
  • Pandas的Series对象
  • numpy

实战:

5.2创建 python 文件

"""
输出:一个DataFrame,包含三列
1000个日期作为索引:从2021-01-01开始
数据列:正态分布1000个随机数,loc=0,scale=1
数据列:均匀分布1000个随机数,low=0,high=1
数据列:二项分布1000个随机数,n=1,p=0.2
"""
 
import pandas as pd
import numpy as np
 
#生成索引列,1000天
date_range = pd.date_range(start='2021-01-01', periods=1000)
 
data = {  'norm': np.random.normal(loc=0, scale=1, size=1000),  'uniform': np.random.uniform(low=0, high=1, size=1000),  'binomial': np.random.binomial(n=1, p=0.2, size=1000)
}
df = pd.DataFrame(data=data, index=date_range)
print(df)

5.3运行结果 

                norm   uniform  binomial
2021-01-01  1.387663  0.223985         0
2021-01-02  2.080345  0.704094         0
2021-01-03  1.615880  0.012283         0
2021-01-04  0.523260  0.053396         0
2021-01-05 -0.872305  0.973047         0
...              ...       ...       ...
2023-09-23 -1.601608  0.423913         0
2023-09-24 -0.712566  0.727326         1
2023-09-25 -0.188441  0.879798         0
2023-09-26  2.249404  0.229298         0
2023-09-27  2.132976  0.472873         0

[1000 rows x 3 columns]

到此这篇关于Pandas如何对Categorical类型字段数据统计实战案例的文章就介绍到这了,更多相关Pandas Categorical数据统计内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

Pandas如何对Categorical类型字段数据统计实战案例的更多相关文章

  1. NT IIS下用ODBC连接数据库

    $connection=intodbc_connect建立数据库连接,$query_string="查询记录的条件"如:$query_string="select*fromtable"用$cur=intodbc_exec检索数据库,将记录集放入$cur变量中。再用while{$var1=odbc_result;$var2=odbc_result;...}读取odbc_exec()返回的数据集$cur。最后是odbc_close关闭数据库的连接。odbc_result()函数是取当前记录的指定字段值。

  2. Thinkphp5框架实现获取数据库数据到视图的方法

    这篇文章主要介绍了Thinkphp5框架实现获取数据库数据到视图的方法,涉及thinkPHP5数据库配置、读取、模型操作及视图调用相关操作技巧,需要的朋友可以参考下

  3. 如何在PHP环境中使用ProtoBuf数据格式

    这篇文章主要介绍了如何在PHP环境中使用ProtoBuf数据格式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

  4. Python爬取奶茶店数据分析哪家最好喝以及性价比

    这篇文章主要介绍了用Python告诉你奶茶哪家最好喝性价比最高,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

  5. Android本地存储方法浅析介绍

    这篇文章主要介绍了Android本地存储案例,方法简单可以实现存储并达到节省内存的效果,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

  6. Pandas如何将表格的前几行生成html实战案例

    这篇文章主要介绍了Pandas如何将表格的前几行生成html实战案例,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下

  7. 详解Python如何实现Excel数据读取和写入

    这篇文章主要为大家详细介绍了python如何实现对EXCEL数据进行读取和写入,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  8. php版微信数据统计接口用法示例

    这篇文章主要介绍了php版微信数据统计接口用法,结合实例形式分析了php微信数据统计接口功能及相关的使用技巧,需要的朋友可以参考下

  9. Python自动化办公之Excel数据的写入

    这篇文章主要为大家详细介绍一下Python中excel的写入模块- xlsxwriter,并利用该模块实现Excel数据的写入,感兴趣的小伙伴可以了解一下

  10. pandas如何计算同比环比增长

    这篇文章主要介绍了pandas如何计算同比环比增长,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

随机推荐

  1. 10 个Python中Pip的使用技巧分享

    众所周知,pip 可以安装、更新、卸载 Python 的第三方库,非常方便。本文小编为大家总结了Python中Pip的使用技巧,需要的可以参考一下

  2. python数学建模之三大模型与十大常用算法详情

    这篇文章主要介绍了python数学建模之三大模型与十大常用算法详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感想取得小伙伴可以参考一下

  3. Python爬取奶茶店数据分析哪家最好喝以及性价比

    这篇文章主要介绍了用Python告诉你奶茶哪家最好喝性价比最高,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

  4. 使用pyinstaller打包.exe文件的详细教程

    PyInstaller是一个跨平台的Python应用打包工具,能够把 Python 脚本及其所在的 Python 解释器打包成可执行文件,下面这篇文章主要给大家介绍了关于使用pyinstaller打包.exe文件的相关资料,需要的朋友可以参考下

  5. 基于Python实现射击小游戏的制作

    这篇文章主要介绍了如何利用Python制作一个自己专属的第一人称射击小游戏,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起动手试一试

  6. Python list append方法之给列表追加元素

    这篇文章主要介绍了Python list append方法如何给列表追加元素,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  7. Pytest+Request+Allure+Jenkins实现接口自动化

    这篇文章介绍了Pytest+Request+Allure+Jenkins实现接口自动化的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  8. 利用python实现简单的情感分析实例教程

    商品评论挖掘、电影推荐、股市预测……情感分析大有用武之地,下面这篇文章主要给大家介绍了关于利用python实现简单的情感分析的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

  9. 利用Python上传日志并监控告警的方法详解

    这篇文章将详细为大家介绍如何通过阿里云日志服务搭建一套通过Python上传日志、配置日志告警的监控服务,感兴趣的小伙伴可以了解一下

  10. Pycharm中运行程序在Python console中执行,不是直接Run问题

    这篇文章主要介绍了Pycharm中运行程序在Python console中执行,不是直接Run问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

返回
顶部