溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Pandas怎么將表格的前幾行生成html

發(fā)布時(shí)間:2022-08-23 14:27:15 來(lái)源:億速云 閱讀:124 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“Pandas怎么將表格的前幾行生成html”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Pandas怎么將表格的前幾行生成html”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

一、Pandas如何將表格的前幾行生成html

創(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())

運(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ù)

創(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())

運(yùn)行結(jié)果

-0.2076894596485453
-0.2076894596485453

三、Pandas如何獲取某個(gè)數(shù)據(jù)列最大和最小的5個(gè)數(shù)

創(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))

運(yùn)行結(jié)果

12    1.607623
17    1.404255
19    0.675887
13    0.345030
Name: col2, dtype: float64

16   -1.220877
18   -1.215324
11   -1.003714
8    -0.936607
5    -0.632613
Name: col2, dtype: float64

四、Pandas如何查看客戶是否流失字段的數(shù)據(jù)映射

創(chuàng)建 python 文件

"""
Churn:客戶是否流失
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"]))

運(yùn)行結(jié)果

No     5174
Yes    1869
Name: Churn, dtype: int64

0    5174
1    1869
Name: Churn, dtype: int6

讀到這里,這篇“Pandas怎么將表格的前幾行生成html”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI