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運行結(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運行結(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運行結(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運行結(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探索之靜態(tài)方法和類方法的區(qū)別詳解
這篇文章主要介紹了Python探索之靜態(tài)方法和類方法的區(qū)別詳解,小編覺得還是挺不錯的,這里分享給大家,供需要的朋友參考。2017-10-10Python中Flask-RESTful編寫API接口(小白入門)
這篇文章主要介紹了Python中Flask-RESTful編寫API接口(小白入門),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12python構(gòu)建深度神經(jīng)網(wǎng)絡(luò)(DNN)
這篇文章主要為大家詳細介紹了python構(gòu)建深度神經(jīng)網(wǎng)絡(luò)DNN,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03Python深度學(xué)習(xí)之Keras模型轉(zhuǎn)換成ONNX模型流程詳解
這篇文章主要介紹了Python深度學(xué)習(xí)之Keras模型轉(zhuǎn)換成ONNX模型流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09python實現(xiàn)將excel文件轉(zhuǎn)化成CSV格式
下面小編就為大家分享一篇python實現(xiàn)將excel文件轉(zhuǎn)化成CSV格式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03pytorch: tensor類型的構(gòu)建與相互轉(zhuǎn)換實例
今天小編就為大家分享一篇pytorch: tensor類型的構(gòu)建與相互轉(zhuǎn)換實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07django 解決manage.py migrate無效的問題
今天小編就為大家分享一篇django 解決manage.py migrate無效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05利用Python批量提取Win10鎖屏壁紙實戰(zhàn)教程
這篇文章主要給大家介紹了關(guān)于利用Python批量提取Win10鎖屏壁紙的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03