網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Pandas如何將表格的前幾行生成html實(shí)戰(zhàn)案例_python
作者:菜鳥(niǎo)實(shí)戰(zhàn) ? 更新時(shí)間: 2022-10-19 編程語(yǔ)言一、Pandas如何將表格的前幾行生成html
實(shí)戰(zhàn)場(chǎng)景:Pandas如何將表格的前幾行生成html
1.1主要知識(shí)點(diǎn)
- 文件讀寫(xiě)
- 基礎(chǔ)語(yǔ)法
- Pandas
- numpy
實(shí)戰(zhàn):
1.2創(chuàng)建 python 文件
import numpy as np
import pandas as pd
?
np.random.seed(66)
s1 = pd.Series(np.random.rand(20))
s2 = pd.Series(np.random.randn(20))
df = pd.concat([s1, s2], axis=1)
df.columns = ['col1', 'col2']
# df.head 取前5行
print(df.head(5).to_html())
1.3運(yùn)行結(jié)果?
<table border="1" class="dataframe">
? <thead>
? ? <tr style="text-align: right;">?
? ? ? <th></th>
? ? ? <th>col1</th>
? ? ? <th>col2</th>
? ? </tr>
? </thead>
? <tbody>
? ? <tr>
? ? ? <th>0</th>
? ? ? <td>0.154288</td>
? ? ? <td>-0.180981</td>
? ? </tr>
? ? <tr>
? ? ? <th>1</th>
? ? ? <td>0.133700</td>
? ? ? <td>-0.056043</td>
? ? </tr>
? ? <tr>
? ? ? <th>2</th>
? ? ? <td>0.362685</td>
? ? ? <td>-0.185062</td>
? ? </tr>
? ? <tr>
? ? ? <th>3</th>
? ? ? <td>0.679109</td>
? ? ? <td>-0.610935</td>
? ? </tr>
? ? <tr>
? ? ? <th>4</th>
? ? ? <td>0.194450</td>
? ? ? <td>-0.048804</td>
? ? </tr>
? </tbody>
</table>
二、Pandas如何計(jì)算一列數(shù)字的中位數(shù)
實(shí)戰(zhàn)場(chǎng)景:Pandas如何計(jì)算一列數(shù)字的中位數(shù)
2.1主要知識(shí)點(diǎn)
- 文件讀寫(xiě)
- 基礎(chǔ)語(yǔ)法
- Pandas
- numpy
實(shí)戰(zhàn):
2.2創(chuàng)建 python 文件
import numpy as np
import pandas as pd
?
np.random.seed(66)
s1 = pd.Series(np.random.rand(20))
s2 = pd.Series(np.random.randn(20))
?
df = pd.concat([s1, s2], axis=1)
df.columns = ['col1', 'col2']
?
?
#median直接算中位數(shù)
print(df["col2"].median())
#用50%分位數(shù)
print(df["col2"].quantile())
2.3運(yùn)行結(jié)果
-0.2076894596485453
-0.2076894596485453
三、Pandas如何獲取某個(gè)數(shù)據(jù)列最大和最小的5個(gè)數(shù)
實(shí)戰(zhàn)場(chǎng)景:Pandas如何獲取某個(gè)數(shù)據(jù)列最大和最小的5個(gè)數(shù)
3.1主要知識(shí)點(diǎn)
- 文件讀寫(xiě)
- 數(shù)據(jù)合并
- Pandas
- numpy
實(shí)戰(zhàn):
3.2創(chuàng)建 python 文件
iimport numpy as np
import pandas as pd
?
np.random.seed(66)
s1 = pd.Series(np.random.rand(20))
s2 = pd.Series(np.random.randn(20))
?
#合并兩個(gè)Series到DF
df = pd.concat([s1, s2], axis=1)
df.columns = ['col1', 'col2']
?
# 取最大的五個(gè)數(shù)
?
print(df["col2"].nlargest(5))
print()
# 取最小的五個(gè)數(shù)
print(df["col2"].nsmallest(5))
3.3運(yùn)行結(jié)果
12 ? ?1.607623
17 ? ?1.404255
19 ? ?0.675887
13 ? ?0.345030
Name: col2, dtype: float6416 ? -1.220877
18 ? -1.215324
11 ? -1.003714
8 ? ?-0.936607
5 ? ?-0.632613
Name: col2, dtype: float64
四、Pandas如何查看客戶(hù)是否流失字段的數(shù)據(jù)映射
實(shí)戰(zhàn)場(chǎng)景:Pandas如何查看客戶(hù)是否流失字段的數(shù)據(jù)映射
4.1主要知識(shí)點(diǎn)
- 文件讀寫(xiě)
- 基礎(chǔ)語(yǔ)法
- Pandas
- numpy
4.2創(chuàng)建 python 文件
"""
Churn:客戶(hù)是否流失
Yes -> 1
No -> 0
實(shí)現(xiàn)字符串到數(shù)字的映射
"""
import pandas as pd
df = pd.read_csv("Telco-Customer-Churn.csv")
#返回取值,及其取值多少次
print(df["Churn"].value_counts())
?
df["Churn"] = df["Churn"].map({"Yes": 1, "No": 0})
print()
print(df["Churn"].value_counts())
print(df.describe(include=["category"]))
4.3運(yùn)行結(jié)果
No ? ? 5174
Yes ? ?1869
Name: Churn, dtype: int640 ? ?5174
1 ? ?1869
Name: Churn, dtype: int6
原文鏈接:https://blog.csdn.net/qq_39816613/article/details/126226763
相關(guān)推薦
- 2022-06-08 Spring Cloud Nacos 配置動(dòng)態(tài)刷新
- 2022-05-13 Django-跨域問(wèn)題Csrf
- 2022-11-28 go?mod文件內(nèi)容版本號(hào)簡(jiǎn)單用法詳解_Golang
- 2022-11-30 關(guān)于pyinstaller?打包多個(gè)py文件的問(wèn)題_python
- 2022-06-06 webpack5.6.0解決報(bào)The ‘mode‘ option has not been set,
- 2022-07-24 Golang實(shí)現(xiàn)可重入鎖的示例代碼_Golang
- 2022-07-23 C#線(xiàn)程間通信的異步機(jī)制_C#教程
- 2022-08-01 混淆矩陣Confusion?Matrix概念分析翻譯_其它綜合
- 最近更新
-
- 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)證過(guò)濾器
- Spring Security概述快速入門(mén)
- 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)程分支