返回顶部

20 个好用的 pandas 函数!(下)

[复制链接]
气泡水Lv.2 显示全部楼层 发表于 2021-12-21 16:21:33 |阅读模式 打印 上一主题 下一主题
本帖最后由 气泡水 于 2021-12-21 16:29 编辑

  11、clip()方法

  clip()方法主要是通过设置阈值来改变数据集当中的数值,当数值超过阈值的时候,就做出相应的调整

[Python] 纯文本查看 复制代码
data = {'col_0': [9, -3, 0, -1, 5], 'col_1': [-2, -7, 6, 8, -5]}
df = pd.DataFrame(data)


  output

[Python] 纯文本查看 复制代码
df.clip(lower = -4, upper = 4)


  output

[Python] 纯文本查看 复制代码
   col_0  col_1
0      4     -2
1     -3     -4
2      0      4
3     -1      4
4      4     -4


  我们看到参数lower和upper分别代表阈值的上限与下限,数据集当中超过上限与下限的值会被替代。

  12、filter()方法

  pandas当中的filter()方法是用来筛选出特定范围的数据的,示例如下

[Python] 纯文本查看 复制代码
df = pd.DataFrame(np.array(([1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12])),
                  index=['A', 'B', 'C', 'D'],
                  columns=['one', 'two', 'three'])


  output

[Python] 纯文本查看 复制代码
   one  two  three
A    1    2      3
B    4    5      6
C    7    8      9
D   10   11     12


  我们使用filter()方法来筛选数据

[Python] 纯文本查看 复制代码
df.filter(items=['one', 'three'])


  output

[Python] 纯文本查看 复制代码
   one  three
A    1      3
B    4      6
C    7      9
D   10     12


  我们还可以使用正则表达式来筛选数据

[Python] 纯文本查看 复制代码
df.filter(regex='e$', axis=1)


  output

[Python] 纯文本查看 复制代码
   one  three
A    1      3
B    4      6
C    7      9
D   10     12


  当然通过参数axis来调整筛选行方向或者是列方向的数据

[Python] 纯文本查看 复制代码
df.filter(like='B', axis=0)


  output

[Python] 纯文本查看 复制代码
   one  two  three
B    4    5      6


  13、first()方法

  当数据集当中的行索引是日期的时候,可以通过该方法来筛选前面几行的数据

[Python] 纯文本查看 复制代码
index_1 = pd.date_range('2021-11-11', periods=5, freq='2D')
ts = pd.DataFrame({'A': [1, 2, 3, 4, 5]}, index=index_1)
ts


  output

[Python] 纯文本查看 复制代码
            A
2021-11-11  1
2021-11-13  2
2021-11-15  3
2021-11-17  4
2021-11-19  5


  我们使用first()方法来进行一些操作,例如筛选出前面3天的数据

[Python] 纯文本查看 复制代码
ts.first('3D')


  output

[Python] 纯文本查看 复制代码
            A
2021-11-11  1
2021-11-13  2


  14、isin()方法

  isin()方法主要是用来确认数据集当中的数值是否被包含在给定的列表当中

[Python] 纯文本查看 复制代码
df = pd.DataFrame(np.array(([1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12])),
                  index=['A', 'B', 'C', 'D'],
                  columns=['one', 'two', 'three'])
df.isin([3, 5, 12])


  output

[Python] 纯文本查看 复制代码
     one    two  three
A  False  False   True
B  False   True  False
C  False  False  False
D  False  False   True


       若是数值被包含在列表当中了,也就是3、5、12当中,返回的是True,否则就返回False

  15、df.plot.area()方法

  下面我们来讲一下如何在Pandas当中通过一行代码来绘制图表,将所有的列都通过面积图的方式来绘制

[Python] 纯文本查看 复制代码
df = pd.DataFrame({
    'sales': [30, 20, 38, 95, 106, 65],
    'signups': [7, 9, 6, 12, 18, 13],
    'visits': [20, 42, 28, 62, 81, 50],
}, index=pd.date_range(start='2021/01/01', end='2021/07/01', freq='M'))

ax = df.plot.area(figsize = (10, 5))


  output



  16、df.plot.bar()方法

  下面我们看一下如何通过一行代码来绘制柱状图

[Python] 纯文本查看 复制代码
df = pd.DataFrame({'label':['A', 'B', 'C', 'D'], 'values':[10, 30, 50, 70]})
ax = df.plot.bar(x='label', y='values', rot=20)


  output



  17、df.plot.box()方法

  我们来看一下箱型图的具体的绘制,通过pandas一行代码来实现

[Python] 纯文本查看 复制代码
data = np.random.randn(25, 3)
df = pd.DataFrame(data, columns=list('ABC'))
ax = df.plot.box()


  output



  18、df.plot.pie()方法

  接下来是饼图的绘制
[Python] 纯文本查看 复制代码
df = pd.DataFrame({'mass': [1.33, 4.87 , 5.97],
                   'radius': [2439.7, 6051.8, 6378.1]},
                  index=['Mercury', 'Venus', 'Earth'])
plot = df.plot.pie(y='mass', figsize=(8, 8))


         output



  
【免责声明】本文系转载,来源于关于数据分析与可视化 ,作者俊欣 。转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与联系我们,我们会予以更改或删除相关文章,以保证您的权益!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

达内教育:成立于2002年。致力于面向IT互联网行业,培养软件开发工程师、测试工程师、系统管理员、智能硬件工程师、UI设计师、网络营销、会计等职场人才 达内使命:缔造年轻人的中国梦、缔造达内员工的中国梦 达内愿景:做管理一流的教育公司
  • 商务合作

  • 微信公众号

  • Powered by Discuz! X3.4 | Copyright © 2002-2021, 达内教育 Tedu.cn
  • 京ICP备08000853号-56 |网站地图 | 京公网安备 11010802029508号