六个令人惊叹的Python 库

一、说明

        在过去的几个年头,我一直在广泛使用Python。因此,我一直在寻找令人惊叹的库,可以增强我在数据工程和商业智能项目中的工作。过去,我分享了两篇文章,我最近遇到的五个很酷的数据科学 Python 库六个很酷的 Python 库

        在本文中,我分享了我现在在工作中使用的另外六个令人惊叹的 python 库。

二. Humanize

        Humanize“为数字、日期和时间提供简单、易于阅读的字符串格式。该库的目标是获取数据并使其更加人性化,例如,通过将秒数转换为更具可读性的字符串(如“2 分钟前”)。该库可以通过多种方式格式化数据,包括使用逗号格式化数字、将时间戳转换为相对时间等。

我经常在数据工程项目中使用整数和日期和时间。

2.1 安装

!pip install humanize

2.2 示例(整数)

# Importing library

import humanize
import datetime as dt

# Formatting  numbers with comma
a =  humanize.intcomma(951009)

# converting numbers into words
b = humanize.intword(10046328394)

#printing

print(a)
print(b)

  输出

2.4 示例(日期和时间)

import humanize
import datetime as dt
    
a = humanize.naturaldate(dt.date(2012, 6, 5))
b = humanize.naturalday(dt.date(2012, 6, 5))

print(a)
print(b)

 输出

        有关更多格式选项,请查看此处

三、 Pendulum

        尽管 Python 中有许多库可用于日期时间,但我发现 Pendulum 在日期的任何操作上都易于使用。钟摆是常工作中最喜欢的图书馆。它扩展了内置的 Python 日期时间模块,添加了更直观的 API,用于处理时区并对日期和时间执行操作,例如添加时间间隔、减去日期和在时区之间转换。它提供了一个简单、人性化的 API 来格式化日期和时间。

3.1 安装

!pip install pendulum 

3.2 例


# import library
import pendulum

dt = pendulum.datetime(2023, 1, 31)
print(dt)
 
#local() creates datetime instance with local timezone

local = pendulum.local(2023, 1, 31)
print("Local Time:", local)
print("Local Time Zone:", local.timezone.name)

# Printing UTC time
utc = pendulum.now('UTC')
print("Current UTC time:", utc)
 
# Converting UTC timezone into Europe/Paris time

europe = utc.in_timezone('Europe/Paris')
print("Current time in Paris:", europe)

输出

        我需要为这个库写一个单独的博客,在这里展示一些例子。有关更多格式,请查看此处

四、FTFY

        您是否遇到过数据中存在的外语无法正确显示的情况?这被称为莫吉巴克。Mojibake 是一个术语,用于描述由于编码或解码问题而发生的乱码或乱码文本。当使用一种字符编码编写的文本使用不同的编码错误解码时,通常会发生这种情况。ftfy python库将帮助您修复Mojibake,这在NLP用例中非常有用。

4.1 安装

!pip install ftfy 

4.2 例

print(ftfy.fix_text('Correct the sentence using “ftfyâ€\x9d.'))
print(ftfy.fix_text('✔ No problems with text'))
print(ftfy.fix_text('à perturber la réflexion'))

 输出

        除了Mojibake,ftfy将修复不正确的编码,不正确的行尾和不正确的引号。根据文档,ftfy 可以理解解码为以下任何编码的文本:

  • 拉丁语-1 (ISO-8859–1)
  • Windows-1252 (cp1252 — 用于Microsoft产品)
  • Windows-1251 (cp1251 — cp1252的俄语版本)
  • Windows-1250 (cp1250 — cp1252的东欧版本)
  • ISO-8859–2(与Windows-1250不完全相同)
  • MacRoman(在 Mac OS 9 及更早版本上使用)
  • cp437(用于 MS-DOS 和某些版本的 Windows 命令提示符)

        有关更多详细信息,请查看此处的文档。

五、Sketch

        该草图是一个独特的AI代码编写助手,专为使用Python中的pandas库的用户而设计。它利用机器学习算法来理解用户数据的上下文,并提供相关的代码建议,使数据操作和分析任务更容易、更高效。Sketch不需要用户在他们的IDE中安装任何其他插件,因此可以快速轻松地开始使用。这可以大大减少数据相关任务所需的时间和精力,并帮助用户编写更好、更高效的代码。

5.1 安装

!pip install sketch 

5.2 例

        我们需要在 Pandas 数据框中添加一个 .sketch 扩展名才能使用此库。

5.2.1 .sketch.ask

        ask是Sketch的一项功能,允许用户以自然语言格式询问有关其数据的问题。它为用户的查询提供基于文本的响应。

# Importing libraries

import sketch
import pandas as pd

# Reading the data (using twitter data as an example)

df = pd.read_csv("tweets.csv")
print(df)

图片来源:作者

# Asking which columns are category type
df.sketch.ask("Which columns are category type?")

输出

# To find the shape of the dataframe
df.sketch.ask("What is the shape of the dataframe")

5.2.2 .sketch.howto

        HowTo 是一项功能,它提供了一个代码块,可用作各种数据相关任务的起点或结论。我们可以要求代码片段来规范化它们的数据、创建新特征、绘制数据,甚至构建模型。这将节省时间并轻松复制和粘贴代码;您无需从头开始手动编写代码。

# To find the shape of the dataframe
df.sketch.ask("What is the shape of the dataframe")

输出

图片来源:作者

5.2.3 .sketch.howto

        .apply 函数有助于生成新特征、解析字段和执行其他数据操作。要使用此功能,我们需要拥有一个 OpenAI 帐户并使用 API 密钥来执行任务。我还没有尝试过这个功能。

        我喜欢使用这个库,尤其是如何操作,我发现它很有用。

        请查看此Github以获取有关Sketch的更多信息。

六. pgeocode

        “pgeocode”是我最近遇到的一个优秀的库,它对我的空间分析项目非常有用。例如,它允许您查找两个邮政编码之间的距离,并通过输入国家/地区和邮政编码来提供地理信息。

6.1 安装

!pip install pgeocode

6.2 例

        获取特定邮政编码的地理信息

# Checking for country "India"

nomi = pgeocode.Nominatim('In')

# Getting geo information by passing the postcodes

nomi.query_postal_code(["620018", "620017", "620012"])

输出

        “PGEOCODE”通过将国家和邮政编码作为输入来计算两个邮政编码之间的距离。结果以公里为单位。

# Finding a distance between two postcodes

distance = pgeocode.GeoDistance('In')

distance.query_postal_code("620018", "620012")

输出

欲了解更多信息,请查看此处

七. rembg

rembg 是另一个有用的库,可以轻松地从图像中删除背景。

7.1 安装

!pip install rembg 

7.2 例

# Importing libraries 
from rembg import remove
import cv2 
# path of input image (my file: image.jpeg)
input_path = 'image.jpeg'
# path for saving output image and saving as a output.jpeg
output_path = 'output.jpeg'
# Reading the input image
input = cv2.imread(input_path)
# Removing background
output = remove(input)
# Saving file 
cv2.imwrite(output_path, output)

输出

        您可能已经熟悉其中一些库,但对我来说,Sketch、Pendulum、pgeocode 和 ftfy 对于我的数据工程工作是必不可少的。我的项目非常依赖它们。

 

你可能也喜欢我之前的文章 五个很酷的数据科学 Python 库 和 我最近遇到的六个很酷的 Python 库

猜你喜欢

转载自blog.csdn.net/gongdiwudu/article/details/132006901