In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to use pipe () to improve code readability in pandas". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use pipe () to improve the readability of code in pandas.
1. Brief introduction
When we use pandas to carry out data analysis, we should try our best to avoid too "fragmented" organizational code, especially to create too many unnecessary "intermediate variables", which not only wastes "memory", but also brings trouble about variable naming, and is not conducive to the readability of the overall analysis process code, so it is very necessary to organize the code in an pipelined way.
Figure 1
In previous articles, I have introduced eval () and query () in pandas to help us write chain code and build a practical API for data analysis workflow. Coupled with the pipe () to be introduced below, we can perfectly organize any pandas code into an assembly line.
two。 Flexible use of pipe () pipe () in pandas
As the name implies, API is specifically used for pipeline transformation of Series and DataFrame operations. Its function is to transform a nested function call procedure into a "chained" procedure, and its first parameter, func, acts on the function corresponding to Series or DataFrame.
Specifically, pipe () can be used in two ways. In the "first mode", the parameter in the first position corresponding to the input function must be the target Series or DataFrame, and other related parameters can be passed in the regular "key-value pair" way. As in the following example, we do some basic feature engineering processing on the Titanic data set by our own function:
Import pandas as pd train = pd.read_csv ('train.csv') def do_something (data, dummy_columns):' 'self-made example function' data = (pd # generates dumb variables for the specified column. Get _ dummies (data, # delete the specified column columns=dummy_columns in data first Drop_first=True) return data # chained pipeline (train # converts Pclass columns to characters for later dumb variable processing. Eval ('PclassPclass=Pclass.astype ("str"), engine='python') # deletes the specified column .drop (columns= [' PassengerId', 'Name',' Cabin') 'Ticket']) # use pipe to call the self-designed function in a chained way.pipe (do_something, dummy_columns= [' Pclass', 'Sex',' Embarked']) # deletes the line with missing values.dropna ())
As you can see, in pipe (), the next step of drop (), we pass in the self-designed function as its first argument, thus subtly embedding a series of operations into the chained process.
"second usage" is suitable for situations where the target Series and DataFrame are not the first parameter of the passed function. For example, in the following example, if we assume that the target input data is the second parameter data2, then the first parameter of pipe () should be passed in the format (function name, parameter name):
Def do_something (data1, data2, axis):''self-made example function' 'data = (pd. Concat ([data1, data2], axisaxis=axis)) return data # pipe () second usage (train. Pipe (do_something,' data2'), data1=train, axis=0))
Under this design, we can avoid many nested function calls and optimize our code at will.
At this point, I believe you have a deeper understanding of "how to use pipe () in pandas to improve the readability of code". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.