In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to achieve Pandas type conversion for python data processing". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve Pandas type conversion for python data processing" can help you solve the problem.
Convert to string type tips ['sex_str'] = tips [' sex'] .astype (str)
Convert to a numeric type
Converting to a numeric type can also use the to_numeric () function
The data type of each column of DataFrame must be the same. When some data is missing but not NaN (such as missing,null, etc.), the whole column of data will become a string type rather than a numeric type. In this case, you can use to_numeric processing.
# create data with a missing value of 'missing'' tips_sub_miss = tips.head (10) tips_sub_miss.loc [[1 total_bill'] = 'missing'tips_sub_miss]
Automatically converted to string type:
Use astype conversion to report an error:
Tips_sub_miss ['total_bill'] .astype (float)
Use the to_numeric () function:
Using the to_numeric () function directly will still report an error. Add the errors parameter.
Errors variable parameters:
Ignore skips when it encounters an error (just skips without changing the type)
When coerce encounters a strong value that cannot be converted to NaN,
Pd.to_numeric (tips_sub_miss ['total_bill'], errors='ignore')
Pd.to_numeric (tips_sub_miss ['total_bill'], errors='coerce')
To_numeric is transforming downwards:
Downcast parameter
The smallest signed int dtype of integer and signed
The smallest float dtype of float
The smallest unsigned int dtype for unsigned
When the downcast parameter is set to float, the data type of total_bill changes from float64 to float32
Pd.to_numeric (tips_sub_miss ['total_bill'], errors='coerce',downcast='float')
Classified data (Category)
Using pd.Categorical () to create categorical data, Categorical () commonly uses three parameters
Ref 1 values. If the value in values is not in the categories parameter, it will be replaced by NaN.
Ref 2 categories, which specifies the possible category data
Ref 3 ordered, whether to specify the order
S = pd.Series (pd.Categorical (["a", "b", "c", "d"], categories= ['clockjigma']))
The sorting of classified data is automatically sorted according to classification:
Ordered specifies the order:
From pandas.api.types import CategoricalDtype# creates a classification ordered specifies the order cat = CategoricalDtype (categories= ['Barrier cat print (series_cat.sort_values ()) print (series_cat1.sort_values () specifies that the series_cat1 transformation type is the created classification type.
Data types summarize the characteristics of knowledge point content Numpy 1. Numpy is an efficient scientific computing library, and the data calculation function of Pandas is the encapsulation of Numpy.
2. Ndarray is the basic data structure of Numpy. Many functions and properties of Series and DataFrame of Pandas are the same as ndarray.
3. Numpy is much more efficient than native Python, and supports parallel computing Pandas data type conversion. Pandas has object and category,bool,datetime types in addition to numeric int and float types.
two。 You can use as_type and to_numeric functions for data type conversion Pandas classification data type 1. Category type, can be used for sorting, and can be customized sort order
2. CategoricalDtype can be used to define the order on "how to implement Pandas type conversion for python data processing". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.