網站首頁 編程語言 正文
一、Pandas如何將表格的前幾行生成html
實戰場景:Pandas如何將表格的前幾行生成html
1.1主要知識點
- 文件讀寫
- 基礎語法
- Pandas
- numpy
實戰:
1.2創建 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運行結果?
<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如何計算一列數字的中位數
實戰場景:Pandas如何計算一列數字的中位數
2.1主要知識點
- 文件讀寫
- 基礎語法
- Pandas
- numpy
實戰:
2.2創建 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直接算中位數
print(df["col2"].median())
#用50%分位數
print(df["col2"].quantile())
2.3運行結果
-0.2076894596485453
-0.2076894596485453
三、Pandas如何獲取某個數據列最大和最小的5個數
實戰場景:Pandas如何獲取某個數據列最大和最小的5個數
3.1主要知識點
- 文件讀寫
- 數據合并
- Pandas
- numpy
實戰:
3.2創建 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))
?
#合并兩個Series到DF
df = pd.concat([s1, s2], axis=1)
df.columns = ['col1', 'col2']
?
# 取最大的五個數
?
print(df["col2"].nlargest(5))
print()
# 取最小的五個數
print(df["col2"].nsmallest(5))
3.3運行結果
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如何查看客戶是否流失字段的數據映射
實戰場景:Pandas如何查看客戶是否流失字段的數據映射
4.1主要知識點
- 文件讀寫
- 基礎語法
- Pandas
- numpy
4.2創建 python 文件
"""
Churn:客戶是否流失
Yes -> 1
No -> 0
實現字符串到數字的映射
"""
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運行結果
No ? ? 5174
Yes ? ?1869
Name: Churn, dtype: int640 ? ?5174
1 ? ?1869
Name: Churn, dtype: int6
原文鏈接:https://blog.csdn.net/qq_39816613/article/details/126226763
相關推薦
- 2022-07-10 解析html中常見的轉義字符
- 2022-05-20 springboot心跳機制,定時任務
- 2022-05-19 Python學習之異常斷言詳解_python
- 2022-09-22 解決ubuntu18.04 磁盤空間不足
- 2022-05-29 Docker鏡像與容器的導入導出操作實踐_docker
- 2022-07-13 python版jpeg合成pdf兩種方法
- 2022-04-18 python使用py2neo創建neo4j的節點和關系_python
- 2022-11-17 Python?隊列Queue和PriorityQueue解析_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支