為什么我們需要使用Pandas新字符串Dtype代替文本數據對象

We have to represent every bit of data in numerical values to be processed and analyzed by machine learning and deep learning models. However, strings do not usually come in a nice and clean format and require a lot preprocessing.

我們必須以數值表示數據的每一位,以便通過機器學習和深度學習模型進行處理和分析。 但是,字符串通常不會采用簡潔的格式,并且需要大量預處理。

Pandas provides numerous functions and methods to process textual data. In this post, we will focus on data types for strings rather than string operations. Using appropriate data types is the first step to make most out of Pandas. There are currently two data types for textual data, object and StringDtype.

熊貓提供了多種功能和方法來處理文本數據。 在本文中,我們將重點介紹字符串的數據類型,而不是字符串操作。 使用適當的數據類型是充分利用Pandas的第一步。 當前,文本數據有兩種數據類型: objectStringDtype。

Before pandas 1.0, only “object” datatype was used to store strings which cause some drawbacks because non-string data can also be stored using “object” datatype. Pandas 1.0 introduces a new datatype specific to string data which is StringDtype. As of now, we can still use object or StringDtype to store strings but in the future, we may be required to only use StringDtype.

在pandas 1.0之前,僅使用“對象”數據類型來存儲字符串,這會導致一些缺點,因為非字符串數據也可以使用“對象”數據類型來存儲。 Pandas 1.0引入了特定于字符串數據的新數據類型StringDtype 。 到目前為止,我們仍然可以使用object或StringDtype來存儲字符串,但是將來,可能會要求我們僅使用StringDtype。

One important thing to note here is that object datatype is still the default datatype for strings. To use StringDtype, we need to explicitly state it.

這里要注意的一件事是對象數據類型仍然是字符串的默認數據類型。 要使用StringDtype,我們需要明確聲明它。

Image for post

We can pass “string” or pd.StringDtype() argument to dtype parameter to select string datatype.

我們可以將“ string ”或pd.StringDtype()參數傳遞給dtype參數以選擇字符串數據類型。

Image for post

We can also convert from “object” to “string” data type using astype function:

我們還可以使用astype函數將“ object”數據類型轉換為“ string”數據類型:

Image for post

Although the default type is “object”, it is recommended to use “string” for a few reasons.

盡管默認類型為“對象”,但出于一些原因,建議使用“字符串”。

  • Object data type has a broader scope and allows to store pretty much anything. Thus, even if we have non-strings in a place that is supposed to be a string, we don’t get any error.

    對象數據類型的范圍更廣,可以存儲幾乎所有內容。 因此,即使我們在應該是字符串的地方放置了非字符串,也不會出現任何錯誤。
Image for post
  • It is always better to have a dedicated data type. For instance, if we try to the example above with “string” data type, we get a TypeError.

    最好使用專用的數據類型。 例如,如果我們嘗試上面的示例使用“字符串”數據類型,則會得到TypeError。
Image for post
  • Having a dedicated data type allows for data type specific operations. For instance, we cannot use select_dtypes to choose only text columns if “object” data type is used. Select_dtypes(include=”object”) will return any column with object data type. On the other hand, if we use “string” data type for textual data, select_dtypes(include=”string”) will give just what we need.

    具有專用數據類型允許進行特定于數據類型的操作。 例如,如果使用“對象”數據類型,則不能使用select_dtypes僅選擇文本列。 Select_dtypes(include =“ object”)將返回任何具有對象數據類型的列。 另一方面,如果我們對文本數據使用“字符串”數據類型,則select_dtypes(include =“ string”)會滿足我們的需求。

“String” data type is not superior to “object” in terms of performance as of now. However, it is expected, with future enhancements, the performance of “string” data type will be increased and the memory consumption will be decreased. Thus, we should already be using “string” instead of “object” for textual data.

到目前為止,就性能而言,“字符串”數據類型并不優于“對象”。 但是,可以預料,隨著將來的增強,“字符串”數據類型的性能將得到提高,內存消耗將減少。 因此,我們應該已經在文本數據中使用“字符串”而不是“對象”。

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

感謝您的閱讀。 如果您有任何反饋意見,請告訴我。

翻譯自: https://towardsdatascience.com/why-we-need-to-use-pandas-new-string-dtype-instead-of-object-for-textual-data-6fd419842e24

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392143.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392143.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392143.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

遞歸方程組解的漸進階的求法——代入法

遞歸方程組解的漸進階的求法——代入法 用這個辦法既可估計上界也可估計下界。如前面所指出,方法的關鍵步驟在于預先對解答作出推測,然后用數學歸納法證明推測的正確性。 例如,我們要估計T(n)的上界,T(n)滿足遞歸方程:…

【轉載】C# 理解泛型

術語表 generics:泛型type-safe:類型安全collection: 集合compiler:編譯器run time:程序運行時object: 對象.NET library:.Net類庫value type: 值類型box: 裝箱unbox: 拆箱implicity: 隱式explicity: 顯式linked list:…

javascript 作用_JavaScript承諾如何從內到外真正發揮作用

javascript 作用One of the most important questions I faced in interviews was how promises are implemented. Since async/await is becoming more popular, you need to understand promises.我在采訪中面臨的最重要的問題之一是如何實現承諾。 由于異步/等待變得越來越流…

linux 文件理解,對linux中文件系統的理解

首先在linux系統當中一個可被掛在的數據為一個文件系統1.在安裝linux過程中我們要進行磁盤分區,可以分根目錄/,‘/home‘,‘/boot’,swap等等這些分區,每一個分區(’/(根目錄)‘,’/home‘...)就是一個文件系統。2.文件系統分配完…

編譯原理—語法分析器(Java)

遞歸下降語法分析 1. 語法成分說明 <語句塊> :: begin<語句串> end <語句串> :: <語句>{&#xff1b;<語句>} <語句> :: <賦值語句> | <循環語句> | <條件語句> <關系運算符> :: < | < | > | > | |…

老筆記整理四:字符串的完美度

今天在寵果網上發現一道題目&#xff0c;求一個字符串的完美度http://hero.pongo.cn/home/index覺得這道題很有趣就挑戰了一下&#xff0c;結果沒有在規定的1小時里面寫完&#xff08;笑&#xff09;&#xff0c;多花了10分鐘終于做出來了。題目是這樣的&#xff1a;我們要給每…

nlp構建_使用NLP構建自殺性推文分類器

nlp構建Over the years, suicide has been one of the major causes of death worldwide, According to Wikipedia, Suicide resulted in 828,000 global deaths in 2015, an increase from 712,000 deaths in 1990. This makes suicide the 10th leading cause of death world…

域名跳轉

案例&#xff1a;當訪問lsx.com網站&#xff0c;是我最早論壇的域名。回車之后會自動跳轉到lshx.com。 為什么藥lsx跳轉到lshx.com呢&#xff1f; 為了統一品牌。建議換成了lshx.com。所有之前的lsx.com就不要用了&#xff0c;就讓它跳轉到lshx.com。是因為之前lsx.com上有很多…

Elastic Stack 安裝

Elastic Stack 是一套支持數據采集、存儲、分析、并可視化全面的分析工具&#xff0c;簡稱 ELK&#xff08;Elasticsearch&#xff0c;Logstash&#xff0c;Kibana&#xff09;的縮寫。 安裝Elastic Stack 時&#xff0c;必須相關組件使用相同的版本&#xff0c;例如&#xff1…

區塊鏈去中心化分布式_為什么漸進式去中心化是區塊鏈的最大希望

區塊鏈去中心化分布式by Arthur Camara通過亞瑟卡馬拉(Arthur Camara) 為什么漸進式去中心化是區塊鏈的最大希望 (Why Progressive Decentralization is blockchain’s best hope) 不變性是區塊鏈的最大優勢和最大障礙。 逐步分權可能是答案。 (Immutability is blockchain’s…

編譯原理—語義分析(Java)

遞歸下降語法制導翻譯 實現含多條簡單賦值語句的簡化語言的語義分析和中間代碼生成。 測試樣例 begin a:2; b:4; c:c-1; area:3.14*a*a; s:2*3.1416*r*(hr); end #詞法分析 public class analyzer {public static List<String> llistnew ArrayList<>();static …

linux問題總結

linux問題總結 編寫后臺進程的管理腳本&#xff0c;使用service deamon-name stop的時候&#xff0c;出現如下提示&#xff1a;/sbin/service: line 66: 23299 Terminated env -i LANG"$LANG" PATH"$PATH" TERM"$TERM" "${SERVICEDIR}/${SE…

linux vi行尾總是顯示顏色,【轉載】Linux 下使用 vi 沒有顏色的解決辦法

vi 是沒有顏色的&#xff0c;vim 是有顏色的。我們可以通過 rpm -qa |grep vim 看看系統中是否安裝了下面 3 個 rpm 包&#xff0c;如果有就是安裝了 vim 。[rootBetty ~]# rpm -qa |grep vimvim-minimal-7.0.109-7.el5vim-enhanced-7.0.109-7.el5vim-common-7.0.109-7.el5如果…

時間序列分析 lstm_LSTM —時間序列分析

時間序列分析 lstmNeural networks can be a hard concept to wrap your head around. I think this is mostly due to the fact that they can be used for so many different things such as classification, identification or just simply regression.神經網絡可能是一個難…

關于計算圓周率PI的經典程序

短短幾行代碼&#xff0c;卻也可圈可點。如把變量s放在PI表達式中&#xff0c;還有正負值的處理&#xff0c;都堪稱經典。尤其是處處考慮執行效率的思想令人敬佩。 /* pi/41-1/31/5-1/71/9-…… */ #include <stdio.h> int main(){ int s1; float pi0.,n1.,…

華為產品技術學習筆記之路由原理(一)

路由器&#xff1a;路由器是一種典型的網絡連接設備&#xff0c;用來進行路由選擇和報文轉發。路由器與它直接相連的網絡的跳數為0&#xff0c;通過一臺路由器可達的網絡的跳數為1.路由協議&#xff1a;路由器之間維護路由表的規則&#xff0c;用以發現路由&#xff0c;生成路由…

Linux網絡配置:設置IP地址、網關DNS、主機名

查看網絡信息 1、ifconfig eth0 2、ifconfig -a 3、ip add 設置主機名需改配置文件&#xff1a; /etc/hosts /etc/sysconfig/network vim /etc/sysconfig/network NETWORKINGyes NETWORKING_IPV6no HOSTNAMEwendyhost Linux配置網絡 方法一&#xff1a; 1、使用setup命令進入如…

編譯原理—小型(簡化)高級語言分析器前端(Java)

實現一個一遍掃描的編譯前端&#xff0c;將簡化高級語言的部分語法成分&#xff08;含賦值語句、分支語句、循環語句等&#xff09;翻譯成四元式&#xff08;或三地址代碼&#xff09;&#xff0c;還要求有合理的語法出錯報錯和錯誤恢復功能。 測試樣例 beginwhile a<b do…

linux boot菜單列表,Bootstrap 下拉菜單(Dropdowns)簡介

Bootstrap 下拉菜單是可切換的&#xff0c;是以列表格式顯示鏈接的上下文菜單。這可以通過與 下拉菜單(Dropdown) JavaScript 插件 的互動來實現。如需使用下拉菜單&#xff0c;只需要在 class .dropdown 內加上下拉菜單即可。下面的實例演示了基本的下拉菜單&#xff1a;實例主…

dynamodb管理ttl_如何使用DynamoDB TTL和Lambda安排臨時任務

dynamodb管理ttlby Yan Cui崔燕 如何使用DynamoDB TTL和Lambda安排臨時任務 (How to schedule ad-hoc tasks with DynamoDB TTL and Lambda) CloudWatch Events let you easily create cron jobs with Lambda. However, it’s not designed for running lots of ad-hoc tasks,…