Pandas如何將表格的前幾行生成html實戰(zhàn)案例
一、Pandas如何將表格的前幾行生成html
實戰(zhàn)場景:Pandas如何將表格的前幾行生成html
1.1主要知識點
- 文件讀寫
- 基礎(chǔ)語法
- Pandas
- numpy
實戰(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如何計算一列數(shù)字的中位數(shù)
實戰(zhàn)場景:Pandas如何計算一列數(shù)字的中位數(shù)
2.1主要知識點
- 文件讀寫
- 基礎(chǔ)語法
- Pandas
- numpy
實戰(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如何獲取某個數(shù)據(jù)列最大和最小的5個數(shù)
實戰(zhàn)場景:Pandas如何獲取某個數(shù)據(jù)列最大和最小的5個數(shù)
3.1主要知識點
- 文件讀寫
- 數(shù)據(jù)合并
- Pandas
- numpy
實戰(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)) ? #合并兩個Series到DF df = pd.concat([s1, s2], axis=1) df.columns = ['col1', 'col2'] ? # 取最大的五個數(shù) ? print(df["col2"].nlargest(5)) print() # 取最小的五個數(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如何查看客戶是否流失字段的數(shù)據(jù)映射
實戰(zhàn)場景:Pandas如何查看客戶是否流失字段的數(shù)據(jù)映射
4.1主要知識點
- 文件讀寫
- 基礎(chǔ)語法
- Pandas
- numpy
4.2創(chuàng)建 python 文件
"""
Churn:客戶是否流失
Yes -> 1
No -> 0
實現(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
到此這篇關(guān)于Pandas如何將表格的前幾行生成html實戰(zhàn)案例的文章就介紹到這了,更多相關(guān)Pandas生成html內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用python實現(xiàn)excel的Vlookup功能
這篇文章主要介紹了使用python實現(xiàn)excel的Vlookup功能,當(dāng)我們想要查找的數(shù)據(jù)量較大時,這時則有請我們的主角VLookup函數(shù)出場,那么如何用python實現(xiàn)VLookup呢,需要的朋友可以參考下2023-04-04
python中的subprocess.Popen()使用詳解
今天小編就為大家分享一篇python中的subprocess.Popen()使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python?Setuptools的?setup.py實例詳解
setup.py是一個?python?文件,它的存在表明您要安裝的模塊/包可能已經(jīng)用?Setuptools?打包和分發(fā),這是分發(fā)?Python?模塊的標(biāo)準(zhǔn)。?它的目的是正確安裝軟件,本文給大家講解Python?Setuptools的?setup.py感興趣的朋友跟隨小編一起看看吧2022-12-12
pytorch獲取模型某一層參數(shù)名及參數(shù)值方式
今天小編就為大家分享一篇pytorch獲取模型某一層參數(shù)名及參數(shù)值方式,具有很好的價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
詳解Numpy擴(kuò)充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方
這篇文章主要介紹了詳解Numpy擴(kuò)充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

