Chapter 12. Advanced pandas
The preceding chapters have focused on introducing different types of data wrangling workflows and features of NumPy, pandas, and other libraries. Over time, pandas has developed a depth of features for power users. This chapter digs into a few more advanced feature areas to help you deepen your expertise as a pandas user.
12.1 Categorical Data
This section introduces the pandas Categorical type. I will show how
you can achieve better performance and memory use in some pandas
operations by using it. I also introduce some tools for using categorical
data in statistics and machine learning applications.
Background and Motivation
Frequently, a column in a table may contain repeated instances of
a smaller set of distinct values. We have already seen functions
like unique and
value_counts, which enable us to extract the distinct
values from an array and compute their frequencies, respectively:
In[12]:importnumpyasnp;importpandasaspdIn[13]:values=pd.Series(['apple','orange','apple',....:'apple']*2)In[14]:valuesOut[14]:0apple1orange2apple3apple4apple5orange6apple7appledtype:objectIn[15]:pd.unique(values)Out[15]:array(['apple','orange'],dtype=object)In[16]:pd.value_counts(values)Out[16]:apple6orange2dtype:int64
Many data systems (for data warehousing, statistical computing, or other uses) have developed specialized approaches for representing data with repeated values for more efficient storage and computation. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access