Pandas怎么将表格的前几行生成html

发布时间:2022-08-23 14:27:15 作者:iii
来源:亿速云 阅读:115

本文小编为大家详细介绍“Pandas怎么将表格的前几行生成html”,内容详细,步骤清晰,细节处理妥当,希望这篇“Pandas怎么将表格的前几行生成html”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

一、Pandas如何将表格的前几行生成html

创建 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())

运行结果 

<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如何计算一列数字的中位数

创建 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())

运行结果

-0.2076894596485453
-0.2076894596485453

三、Pandas如何获取某个数据列最大和最小的5个数

创建 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))

运行结果

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如何查看客户是否流失字段的数据映射

创建 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"]))

运行结果

No     5174
Yes    1869
Name: Churn, dtype: int64

0    5174
1    1869
Name: Churn, dtype: int6

读到这里,这篇“Pandas怎么将表格的前几行生成html”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. python如何打印文件的前几行或最后几行
  2. pandas groupby 分组取每组的前几行记录方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pandas html

上一篇:怎么用filebeat收集json格式的tomcat日志

下一篇:Spring怎么配置数据源

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》