日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(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: float64

16 ? -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: int64

0 ? ?5174
1 ? ?1869
Name: Churn, dtype: int6

原文鏈接:https://blog.csdn.net/qq_39816613/article/details/126226763

欄目分類(lèi)
最近更新