七个鲜为人知的搜索网站_4个鲜为人知但功能强大的熊猫行动

news/2024/5/10 3:37:40/文章来源:https://blog.csdn.net/weixin_26713457/article/details/109070189

七个鲜为人知的搜索网站

Pandas being the most widely used data analysis and manipulation library provides numerous functions and methods to work with data. Some of them are used more frequently than others because of the tasks they perform.

熊猫是使用最广泛的数据分析和处理库,它提供了许多处理数据的功能和方法。 由于它们执行的任务,它们中的一些比其他使用更频繁。

In this post, we will cover 4 pandas operations that are less frequently used but still very functional.

在本文中,我们将介绍4种不常用的熊猫操作,但它们仍然非常有用。

Let’s start with importing NumPy and Pandas.

让我们从导入NumPy和Pandas开始。

import numpy as np
import pandas as pd

1.分解 (1. Factorize)

It provides a simple way to encode categorical variables which is a required task in most machine learning techniques.

它提供了一种编码分类变量的简单方法,这是大多数机器学习技术中必需的任务。

Here is a categorical variable from a customer churn dataset.

这是来自客户流失数据集的分类变量。

df = pd.read_csv('/content/Churn_Modelling.csv')df['Geography'].value_counts()
France 5014
Germany 2509
Spain 2477
Name: Geography, dtype: int64

We can encode the categories (i.e. convert to numbers) with just one line of code.

我们可以只用一行代码对类别进行编码(即转换为数字)。

df['Geography'], unique_values = pd.factorize(df['Geography'])

The factorize function returns the converted values along with an index of categories.

factorize函数返回转换后的值以及类别索引。

df['Geography'].value_counts()
0 5014
2 2509
1 2477
Name: Geography, dtype: int64unique_values
Index(['France', 'Spain', 'Germany'], dtype='object')

If there are missing values in the original data, you can specify a value to be used for them. The default value is -1.

如果原始数据中缺少值,则可以指定要用于它们的值。 默认值为-1。

A = ['a','b','a','c','b', np.nan]
A, unique_values = pd.factorize(A)
array([ 0, 1, 0, 2, 1, -1])A = ['a','b','a','c','b', np.nan]
A, unique_values = pd.factorize(A, na_sentinel=99)
array([ 0, 1, 0, 2, 1, 99])

2.分类 (2. Categorical)

It can be used to create a categorical variable.

它可用于创建分类变量。

A = pd.Categorical(['a','c','b','a','c'])

The categories attribute is used to access the categories:

Categories属性用于访问类别:

A.categories
Index(['a', 'b', 'c'], dtype='object')

We can only assign new values from one of the existing categories. Otherwise, we will get a value error.

我们只能从现有类别之一分配新值。 否则,我们将获得值错误。

A[0] = 'd'
Image for post

We can also specify the data type using the dtype parameter. The default is the CategoricalDtype which is actually the best one use because of memory consumption.

我们还可以使用dtype参数指定数据类型。 默认值为CategoricalDtype,实际上这是最好的一种用法,因为它会消耗内存。

Let’s do an example to compare memory usage.

让我们做一个比较内存使用情况的例子。

Image for post

This is the memory usage in bytes for each column.

这是每列的内存使用量(以字节为单位)。

countries = pd.Categorical(df['Geography'])
df['Geography'] = countries
Image for post

The memory usage is 8 times less than the original feature. The amount of memory saved will further increase on larger datasets especially when we have very few categories.

内存使用量比原始功能少8倍。 在较大的数据集上,保存的内存量将进一步增加,尤其是在类别很少的情况下。

3.间隔 (3. Interval)

It returns an immutable object representing an interval.

它返回一个代表间隔的不可变对象。

iv = pd.Interval(left=1, right=5, closed='both')3 in iv
True5 in iv
True

The closed parameter indicates if the bounds are included. The values it takes are “both”, “left”, “right”, and “neither”. The default value is “right”.

close参数指示是否包括边界。 它采用的值是“ both”,“ left”,“ right”和“ noth”。 默认值为“ right”。

iv = pd.Interval(left=1, right=5, closed='neither')5 in iv
False

The interval comes in handy when we are working with date-time data. We can easily check if the dates are in a specified interval.

当我们使用日期时间数据时,该间隔会很方便。 我们可以轻松地检查日期是否在指定的间隔内。

date_iv = pd.Interval(left = pd.Timestamp('2019-10-02'), 
right = pd.Timestamp('2019-11-08'))date = pd.Timestamp('2019-10-10')date in date_iv
True

4.宽到长 (4. Wide_to_long)

Melt converts wide dataframes to long ones. This task can also be done with the melt function. Wide_to_long offers a less flexible but more user-friendly way.

Melt将宽数据帧转换为长数据帧。 该任务也可以通过熔化功能来完成。 Wide_to_long提供了一种不太灵活但更加用户友好的方式。

Consider the following sample dataframe.

考虑以下示例数据帧。

Image for post

It contains different scores for some people. We want to modify (or reshape) this dataframe in a way that the score types are represented in a row (not as a separate column). For instance, there are 3 score types under A (A1, A2, A3). After we convert the dataframe, there will only be on column (A) and types (1,2,3) will be represented with row values.

它对某些人包含不同的分数。 我们希望以分数类型在一行中(而不是在单独的列中)表示的方式修改(或重塑)此数据框。 例如,A下有3种得分类型(A1,A2,A3)。 转换数据框后,将仅在(A)列上,并且类型(1,2,3)将用行值表示。

pd.wide_to_long(df, stubnames=['A','B'], i='names', j='score_type')
Image for post

The stubnames parameter indicates the names of the new columns that will contain the values. The column names in the wide-format need to start with the stubnames. The “i” parameter is the column to be used as the id variable and the ‘j’ parameter is the name of the column that contains subcategories.

stubnames参数指示将包含值的新列的名称。 宽格式的列名称必须以存根名称开头​​。 “ i”参数是用作id变量的列,“ j”参数是包含子类别的列的名称。

The returned dataframe has a multi-level index but we can convert it to a normal index by applying the reset_index function.

返回的数据帧具有多级索引,但是我们可以通过应用reset_index函数将其转换为普通索引。

pd.wide_to_long(df, stubnames=['A','B'], i='names', j='score_type').reset_index()
Image for post

Pandas owes its success and predominance in the field of data science and machine learning to the variety and flexibility of the functions and methods. Some methods perform basic tasks whereas there are also detailed and more specific ones.

熊猫公司在数据科学和机器学习领域的成功和优势归功于功能和方法的多样性和灵活性。 一些方法执行基本任务,但也有详细且更具体的方法。

There are usually multiple ways to do a task with Pandas which makes it easily fit specific tasks well.

通常,有多种方法可以对Pandas执行任务,这使其很容易适应特定任务。

Thank you for reading. Please let me know if you have any feedback.

感谢您的阅读。 如果您有任何反馈意见,请告诉我。

翻译自: https://towardsdatascience.com/4-less-known-yet-very-functional-pandas-operations-46dcf2bd9688

七个鲜为人知的搜索网站

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_747437.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Windows Azure 创建虚拟机并发布测试网站

很多人刚开始使用Windows Azure,希望可以测试最基本的功能:使用虚拟机发布一个测试网站。Azure虚拟机的大部分功能与本地类似,某些设置需要用户在管理门户上做特殊操作。接下来,我们共同完成从创建Win Server 2012R2虚拟机到使用该…

【干货】常用的14个获取数据的网站。

转自:菜J学Python 来源:知乎大家好,数据分析时,除了自家的数据库,免不了要找一些外部的数据来论证某些问题,这里给大家分享14个权威、常用的网站,以备不时之需。1.中华人民共和国统计局国家统计…

大型网站系统特点

2019独角兽企业重金招聘Python工程师标准>>> 一、大型网站系统特点 (1)高并发、大流量:PV量巨大 (2)高可用:7*24小时不间断服务 (3)海量数据:文件数目分分…

PS网页设计教程IV——如何在Photoshop中创建一个专业博客网站布局

向Talk-Mania网站致敬。一年前,在该网站上看过许多不错的网页设计教程。一年后,再回头想看看有没有什么新的教程的时候,蓦然发现该网站已经打不开了。也许是关闭了,也许是改了网站名了。幸好,去年本人还是下载保存了几…

方配网站服务器(FPWebServer) V1.6.22.2

方配网站服务器(FPWebServer)是一款免费轻量级独立安装版的IIS服务器。支持ASP、ASP.NET和其他IIS所支持的文件类型与扩展。支持远程请求,无连接限制,可以用于本地调试使用。简便的安装方式与人性化的管理界面,使用户简单而快速地部署和管理站…

Windows 服务器配置、运行、图文流程(新手必备!) - IIS建站配置一条龙

Window 2008 服务器的配置教程本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chinar —— 心分享、心创新!助力快…

25个精美的个人作品集网站,激发灵感

我的大多数朋友都是网页设计师或开发人员,其中大部分人都已经创建了自己的个人作品集网站来展现技能并链接到他们以前的作品。有一个在线的作品组合,并保持它的更新对于专业人士是至关重要的,它绝不是仅限于那些从事 Web 设计和开发工作的人。…

好家伙,被我发现了个数据结构与算法可视化网站!

‍‍大家早上好呀!之前看到了小林这篇讲数据结构算法可视化网站的文章,觉得很不错,一直想分享给大家,今天总算记得了。正文如下:网上有很多这类数据结构与算法可视化的网站,能够自己输入数据,然…

5个适合新手练习的Python刷题网站

知乎上有人问,有没有适合新手练习 Python 的做题类网站?根据我刷题找资料的经验,推荐以下5个Python练习网站,都很良心1、Github这不是一个专门的刷题网站,而是代码托管平台,里面有数百万个Python项目&#…

10分钟轻松设置出 A+ 评分的 HTTP/2 网站

前言 其实 HTTP/2 应该是 2015 年的老话题了(2015 年 5 月 14 日 HTTP/2 协议正式版的发布),但是 2018 年都到了很多网站依旧没有使用,作为新一代互联网协议,HTTP/2 不仅速度比目前常见的 HTTP/1.1 更快,而…

PublicCMS 网站漏洞 任意文件写入并可提权服务器权限

2019独角兽企业重金招聘Python工程师标准>>> PublicCMS是目前网站系统中第一个采用JAVA架构 TOMCATApccheMysql数据库架构的CMS网站,开源,数据承载量大,可以承载到上千万的数据量,以及用户的网站并发可达到上千万的PV&…

十大抢手的网站压力测试工具

十大抢手的网站压力测试工具 2010-07-21 23:10:52| 分类: 营销推广 |举报 |字号 订阅 两天,jnj在本站发布了《如何在低速率网络中测试 Web 应用》,那是测试网络不好的情况。而下面是十个免费的可以用来进行Web的负载/压力测试的工具&…

使用iis部署一个网站

1、在服务器上找到一个目录,存放网站文件,在这里假设为放到D盘根目录下文件夹webSite里,打开服务器的方式:win7里是,在“开始”菜单里找到“附件”,在附件里找到“远程桌面连接”,输入服务器ip&…

为何大量网站不能抓取?爬虫突破封禁的6种常见方法

在互联网上进行自动数据采集(抓取)这件事和互联网存在的时间差不多一样长。今天大众好像更倾向于用“网络数据采集”,有时会把网络数据采集程序称为网络机器人(bots)。最常用的方法是写一个自动化程序向网络服务器请求…

再聊聊我常用的15个数据源网站

前面介绍过实用的效率小工具,真的帮了我很多忙,这次给小伙伴们再种草一些数据源网站。现在有很多免费的数据可以供使用分析,不过很少有人能找的到,或者没能力找,这就是所谓的信息差吧。其实数据获取分为两方面&#xf…

优化网站设计(十七):延迟或按需加载内容

前言 网站设计的优化是一个很大的话题,有一些通用的原则,也有针对不同开发平台的一些建议。这方面的研究一直没有停止过,我在不同的场合也分享过这样的话题。 作为通用的原则,雅虎的工程师团队曾经给出过35个最佳实践。这个列表请参考 Best Practices f…

企业建站程序哪个好?

企业建站程序推荐使用的Eyoucms企业网站管理系统,同其它同行的企业建站系统来说,Eyoucms企业网站管理系统有些什么优势呢?1、网络的安全性是网民一直担心的问题,可能随时不小心就被黑掉或者被恶意挂上使得网站无法正常运营&#x…

ASP.NET本质论第一章网站应用程序学习笔记1

1.统一资源标示符 1) 格式:协议://主机[.端口号][绝对路径[?参数]],在Http://www.kencery.com/hyl/index/login中,http表示协议的名称,www.kencery.com表示主机的地址,可选的端口号没有出现,那么&#xff…

一个学习数据科学的可视化网站

https://setosa.io/ev/markov-chains/ 可以通过调节样本的参数,理解模型在干些什么

如何查找网站漏洞文件任意查看漏洞详情与利用

2019独角兽企业重金招聘Python工程师标准>>> 在对网站程序代码的安全检测当中,网站文件任意查看漏洞在整个网站安全报告中属于比较高危的网站漏洞,一般网站里都会含有这种漏洞,尤其平台,商城,交互类的网站较…