網(wǎng)站首頁 編程語言 正文
Pandas如何對(duì)Categorical類型字段數(shù)據(jù)統(tǒng)計(jì)實(shí)戰(zhàn)案例_python
作者:菜鳥實(shí)戰(zhàn) ? 更新時(shí)間: 2022-10-19 編程語言一、Pandas如何對(duì)Categorical類型字段數(shù)據(jù)統(tǒng)計(jì)
實(shí)戰(zhàn)場(chǎng)景:對(duì)Categorical類型字段數(shù)據(jù)統(tǒng)計(jì),Categorical類型是Pandas擁有的一種特殊數(shù)據(jù)類型,這樣的類型可以包含基于整數(shù)的類別展示和編碼的數(shù)據(jù)
1.1主要知識(shí)點(diǎn)
- 文件讀寫
- 基礎(chǔ)語法
- Pandas
- read_csv
實(shí)戰(zhàn):
1.2創(chuàng)建 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)
?
# 將分類列轉(zhuǎn)換成 Categorical 類型
number_columns = ['tenure', 'MonthlyCharges', 'TotalCharges']
for column in number_columns: ?df[column] = df[column].astype(float) #對(duì)三列變成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運(yùn)行結(jié)果
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如何從股票數(shù)據(jù)找出收盤價(jià)最低行
實(shí)戰(zhàn)場(chǎng)景:Pandas如何從股票數(shù)據(jù)找出收盤價(jià)最低行
2.1主要知識(shí)點(diǎn)
- 文件讀寫
- 基礎(chǔ)語法
- Pandas
- read_csv
2.2創(chuàng)建 python 文件
"""
數(shù)據(jù)是CSV格式
1、加載到dataframe
2、找出收盤價(jià)最低的索引
3、根據(jù)索引找出數(shù)據(jù)行4 打印結(jié)果數(shù)據(jù)行
"""
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運(yùn)行結(jié)果
? ? ?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如何給股票數(shù)據(jù)新增年份和月份
實(shí)戰(zhàn)場(chǎng)景:Pandas如何給股票數(shù)據(jù)新增年份和月份
3.1主要知識(shí)點(diǎn)
?
- 文件讀寫
- 基礎(chǔ)語法
- Pandas
- Pandas的Series對(duì)象
- DataFrame
實(shí)戰(zhàn):
3.2創(chuàng)建 python 文件
"""
給股票數(shù)據(jù)新增年份和月份
"""
import pandas as pd
?
df = pd.read_csv("./00100.csv")
print(df)
?
# to_datetime變成時(shí)間類型
df["Date"] = pd.to_datetime(df["Date"])
df["Year"] = df["Date"].dt.year
df["Month"] = df["Date"].dt.month
?
print(df)
3.3運(yùn)行結(jié)果
? ? ? ? ? ? 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如何獲取表格的信息和基本數(shù)據(jù)統(tǒng)計(jì)
實(shí)戰(zhàn)場(chǎng)景:Pandas如何獲取表格的信息和基本數(shù)據(jù)統(tǒng)計(jì)
4.1主要知識(shí)點(diǎn)
- 文件讀寫
- 基礎(chǔ)語法
- Pandas
- Pandas的Series對(duì)象
- numpy
實(shí)戰(zhàn):
4.2創(chuàng)建 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(),查看每列的平均值、最小值、最大值、中位數(shù)等統(tǒng)計(jì)信息;
print(df.info())
print()
print(df.describe())
4.3運(yùn)行結(jié)果 ?
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如何使用日期和隨機(jī)數(shù)生成表格數(shù)據(jù)類型
實(shí)戰(zhàn)場(chǎng)景:Pandas如何使用日期和隨機(jī)數(shù)生成表格數(shù)據(jù)類型
5.1主要知識(shí)點(diǎn)
- 文件讀寫
- 基礎(chǔ)語法
- Pandas
- Pandas的Series對(duì)象
- numpy
實(shí)戰(zhàn):
5.2創(chuàng)建 python 文件
"""
輸出:一個(gè)DataFrame,包含三列
1000個(gè)日期作為索引:從2021-01-01開始
數(shù)據(jù)列:正態(tài)分布1000個(gè)隨機(jī)數(shù),loc=0,scale=1
數(shù)據(jù)列:均勻分布1000個(gè)隨機(jī)數(shù),low=0,high=1
數(shù)據(jù)列:二項(xiàng)分布1000個(gè)隨機(jī)數(shù),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運(yùn)行結(jié)果?
? ? ? ? ? ? ? ? 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]
原文鏈接:https://blog.csdn.net/qq_39816613/article/details/126231344
相關(guān)推薦
- 2022-08-15 Property or field ‘xxx‘ cannot be found on object
- 2022-06-07 FreeRTOS實(shí)時(shí)操作系統(tǒng)結(jié)構(gòu)示例_操作系統(tǒng)
- 2022-09-22 Mybaits一級(jí)緩存和二級(jí)緩存分別是什么,區(qū)別是什么?
- 2022-09-15 C#獲取文件名和文件路徑的兩種實(shí)現(xiàn)方式_C#教程
- 2022-06-06 Ubuntu系統(tǒng)-FFmpeg安裝及環(huán)境配置
- 2023-01-18 python中的參數(shù)類型匹配提醒_python
- 2022-09-04 使用Python去除小數(shù)點(diǎn)后面多余的0問題_python
- 2022-07-12 快速上手Vim編輯器
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支