search
HomeBackend DevelopmentPython TutorialSome tips for implementing data type conversion in Pandas

This article mainly introduces some techniques on data type conversion in Pandas, which has certain reference value. Now I share it with you. Friends in need can refer to it

Preface

Pandas is an important data analysis tool in Python. When using Pandas for data analysis, it is very important to ensure that the correct data types are used, otherwise some unpredictable errors may occur. .

Data types in Pandas: Data types are essentially the internal structures that programming languages ​​use to understand how to store and manipulate data. For example, a program needs to understand that you can add two numbers, such as 5 10 to get 15. Or, if it's two strings, such as "cat" and "hat", you can concatenate (add) them to get "cathat". Shangxuetang·Baizhan programmer Mr. Chen pointed out that one potentially confusing thing about Pandas data types is that there is some overlap between the data types of Pandas, Python and numpy.

Most of the time, you don't have to worry about whether you should explicitly cast a pandas type to the corresponding NumPy type. Generally speaking, you can use Pandas' default int64 and float64. The only reason I include this table is that sometimes you might see Numpy types between lines of code or during your own analysis.
Data types are one of those things that you don't care about until you encounter an error or unexpected result. But it’s also the first thing you should check when loading new data into Pandas for further analysis.

The author has been using Pandas for some time, but I still make mistakes on some minor issues. Tracing back to the source, I found that some feature columns are not of the type that Pandas can handle when operating on data. Therefore, this article will discuss some tips on how to convert Python's basic data types into data types that Pandas can handle.

The data types supported by Pandas, Numpy and Python


From the above table It can be seen that Pandas supports the most abundant data types. In some cases, Numpy data types can be converted to Pandas data types. After all, the Pandas library is developed on the basis of Numpy.

Introducing actual data for analysis

The data type is something that you may not care much about until you get the wrong result, so in An example of actual data analysis is introduced here to deepen understanding.

import numpy as np
import pandas as pd

data = pd.read_csv('data.csv', encoding='gbk') #因为数据中含有中文数据
data


The data is loaded. If you want to perform some operations on the data now, such as Add the corresponding items in columns 2016 and 2017.

data['2016'] data['2017'] #Taken for granted


From As a result, the numerical values ​​are not added as expected. This is because the addition of object types in Pandas is equivalent to the addition of strings in Python.

data.info() #Before processing the data, you should first check the relevant information of the loaded data


##After seeing the relevant information about loading data, we can find the following problems:

  • The data type of the customer number is int64 instead of object type

  • The data type of the 2016 and 2017 columns is object instead of numeric type (int64, float64)

  • The data type of the growth rate and the group to which it belongs should be numeric type instead of object type

  • The data types of year, month, and day should be datetime64 type instead of object type

There are three basic methods for data type conversion in Pandas :

  • Use astype() function for forced type conversion

  • Custom function for data type conversion

  • Use the functions provided by Pandas such as to_numeric(), to_datetime()

##Use the astype() function for type conversion


The easiest way to convert the data type of the data column is to use the astype() function

data['客户编号'].astype('object')

data['客户编号'] = data['客户编号'].astype('object') #对原始数据进行转换并覆盖原始数据列

Look at the above results It sounds very good. Here are a few examples where the astype() function works on column data but fails

data['2017'].astype('float')

data['所属组'].astype('int')


从上面两个例子可以看出,当待转换列中含有不能转换的特殊值时(例子中¥,ErrorValue等)astype()函数将失效。有些时候astype()函数执行成功了也并不一定代表着执行结果符合预期(神坑!)

data['状态'].astype('bool')


乍一看,结果看起来不错,但仔细观察后,会发现一个大问题。那就是所有的值都被替换为True了,但是该列中包含好几个N标志,所以astype()函数在该列也是失效的。

总结一下astype()函数有效的情形:

  • 数据列中的每一个单位都能简单的解释为数字(2, 2.12等)

  • 数据列中的每一个单位都是数值类型且向字符串object类型转换

如果数据中含有缺失值、特殊字符astype()函数可能失效。

使用自定义函数进行数据类型转换

该方法特别适用于待转换数据列的数据较为复杂的情形,可以通过构建一个函数应用于数据列的每一个数据,并将其转换为适合的数据类型。

对于上述数据中的货币,需要将它转换为float类型,因此可以写一个转换函数:

def convert_currency(value):
 """
 转换字符串数字为float类型
 - 移除 ¥ ,
 - 转化为float类型
 """
 new_value = value.replace(',', '').replace('¥', '')
 return np.float(new_value)

现在可以使用Pandas的apply函数通过covert_currency函数应用于2016列中的所有数据中。

data['2016'].apply(convert_currency)


该列所有的数据都转换成对应的数值类型了,因此可以对该列数据进行常见的数学操作了。如果利用lambda表达式改写一下代码,可能会比较简洁但是对新手不太友好。

data['2016'].apply(lambda x: x.replace('¥', '').replace(',', '')).astype('float')

当函数需要重复应用于多个列时,个人推荐使用第一种方法,先定义函数还有一个好处就是可以搭配read_csv()函数使用(后面介绍)。

#2016、2017列完整的转换代码
data['2016'] = data['2016'].apply(convert_currency)
data['2017'] = data['2017'].apply(convert_currency)

同样的方法运用于增长率,首先构建自定义函数

def convert_percent(value):
 """
 转换字符串百分数为float类型小数
 - 移除 %
 - 除以100转换为小数
 """
 new_value = value.replace('%', '')
 return float(new_value) / 100

使用Pandas的apply函数通过covert_percent函数应用于增长率列中的所有数据中。

data['增长率'].apply(convert_percent)

使用lambda表达式:

data['增长率'].apply(lambda x: x.replace('%', '')).astype('float') / 100

结果都相同:


为了转换状态列,可以使用Numpy中的where函数,把值为Y的映射成True,其他值全部映射成False。

data['状态'] = np.where(data['状态'] == 'Y', True, False)

同样的你也可以使用自定义函数或者使用lambda表达式,这些方法都可以完美的解决这个问题,这里只是多提供一种思路。

利用Pandas的一些辅助函数进行类型转换

Pandas的astype()函数和复杂的自定函数之间有一个中间段,那就是Pandas的一些辅助函数。这些辅助函数对于某些特定数据类型的转换非常有用(如to_numeric()、to_datetime())。所属组数据列中包含一个非数值,用astype()转换出现了错误,然而用to_numeric()函数处理就优雅很多。

pd.to_numeric(data['所属组'], errors='coerce').fillna(0)


可以看到,非数值被替换成0.0了,当然这个填充值是可以选择的,具体文档见
pandas.to_numeric - pandas 0.22.0 documentation

Pandas中的to_datetime()函数可以把单独的year、month、day三列合并成一个单独的时间戳。

pd.to_datetime(data[['day', 'month', 'year']])

完成数据列的替换

data['new_date'] = pd.to_datetime(data[['day', 'month', 'year']]) #新产生的一列数据
data['所属组'] = pd.to_numeric(data['所属组'], errors='coerce').fillna(0)

到这里所有的数据列都转换完毕,最终的数据显示:


在读取数据时就对数据类型进行转换,一步到位

data2 = pd.read_csv("data.csv",
   converters={
    '客户编号': str,
    '2016': convert_currency,
    '2017': convert_currency,
    '增长率': convert_percent,
    '所属组': lambda x: pd.to_numeric(x, errors='coerce'),
    '状态': lambda x: np.where(x == "Y", True, False)
    },
   encoding='gbk')

在这里也体现了使用自定义函数比lambda表达式要方便很多。(大部分情况下lambda还是很简洁的,笔者自己也很喜欢使用)

Summary

The first step in operating a data set is to ensure that the correct data type is set, and then the data can be analyzed and visualized For other operations, Pandas provides many very convenient functions. With these functions, it will be very convenient to analyze the data.

Related recommendations:

pandas implements selecting rows at a specific index

##

The above is the detailed content of Some tips for implementing data type conversion in Pandas. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python in Action: Real-World ExamplesPython in Action: Real-World ExamplesApr 18, 2025 am 12:18 AM

Python's real-world applications include data analytics, web development, artificial intelligence and automation. 1) In data analysis, Python uses Pandas and Matplotlib to process and visualize data. 2) In web development, Django and Flask frameworks simplify the creation of web applications. 3) In the field of artificial intelligence, TensorFlow and PyTorch are used to build and train models. 4) In terms of automation, Python scripts can be used for tasks such as copying files.

Python's Main Uses: A Comprehensive OverviewPython's Main Uses: A Comprehensive OverviewApr 18, 2025 am 12:18 AM

Python is widely used in data science, web development and automation scripting fields. 1) In data science, Python simplifies data processing and analysis through libraries such as NumPy and Pandas. 2) In web development, the Django and Flask frameworks enable developers to quickly build applications. 3) In automated scripts, Python's simplicity and standard library make it ideal.

The Main Purpose of Python: Flexibility and Ease of UseThe Main Purpose of Python: Flexibility and Ease of UseApr 17, 2025 am 12:14 AM

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python: The Power of Versatile ProgrammingPython: The Power of Versatile ProgrammingApr 17, 2025 am 12:09 AM

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Learning Python in 2 Hours a Day: A Practical GuideLearning Python in 2 Hours a Day: A Practical GuideApr 17, 2025 am 12:05 AM

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.