Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What details should be paid attention to in the process of learning Python

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what details should be paid attention to in the process of learning Python". In daily operation, I believe that many people have doubts about what details should be paid attention to in the process of learning Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what details should be paid attention to in the process of learning Python?" Next, please follow the editor to study!

01. List generator with only one line of code

Wouldn't it be annoying if you had to write a loop every time you wanted to generate a list? Fortunately, Python already has a built-in method that can solve this problem with just one line of code. If you are not familiar with this grammar, it may be a little difficult to understand, but once you get used to this technology, you will love it!

Moving Picture: how to change a Loop to list Generation (Source: Trey Hunner)

The above dynamic diagram is a good example. The original code uses the for loop to generate lists, but the diagram has transformed it step by step into a list generator with only one line of code, no more loops. Isn't it simple?

Here is another example of comparison:

Use loops:

The result of the output is [1,4,9,16]

Use generative:

The output is also [1, 4, 9, 16]

02, Lambda expression

Obviously this function can not be used a few times, each time you have to write a long list of functions to build code, is it not very tired? Don't be afraid, Lambda expressions will save you! Lambda expressions can easily create function objects that are simple, once used, and anonymous. Basically, they allow you to use a function instead of bothering to construct it.

The basic syntax of an Lambda expression is:

Sinicization of European shaving

Keep in mind that the functions created by Lambda expressions are no different from those built by normal def, except that the body of the function is a single expression. Look at the following example:

The output is 10.

03, Map and Filter functions

Once you've mastered Lambda expressions, it's powerful to use them with map or filter functions.

Specifically, the map () function takes a list and a function, which calls a function for each element in the list and puts the result into a new list. In the following example, the map () function iterates through each element in seq, multiplies it by 2, puts the result in a new list, and * returns the list. The outermost list () function converts the object returned by map () into a list format.

The output result is [2, 4, 6, 8, 10]

The filter () function is slightly different. It takes a list and a rule function, and after calling this rule function on each element in the list, it removes all elements that return false values from the list, and then returns the filtered sublist.

The result of the output is [3,4,5]

04, Arange and Linspace functions

In order to generate an array of numpy quickly and easily, you must be familiar with the functions arange () and linspace (). These two functions have their own specific uses, but for us, they are both good at generating numpy arrays (instead of using range ()), which is quite useful in the analysis of data science.

The arange () function returns an arithmetic sequence at the specified step. In addition to the start and end values, you can also customize the step size and data type. Note that the given end value parameter is not included in the result.

The output is an array object: array ([3,5])

The use of the linspace () function is similar, but with a slight difference. Linspace () returns a sequence of equal points that divide a given interval into several equal parts. So the parameters you pass in include the start value, the end value, and the exact number of points. Linspace () divides the interval and puts the start value, the end value, and each bisection point into a NumPy array. This is useful for data visualization and drawing axes.

The output is an array object: array ([2.0,2.25,2.5,2.75,3.0])

05. The significance of coordinate axis (axis parameter) in Pandas

You may have encountered the problem of this axis parameter when you want to filter out a column in Pandas, or when you want to sum the data in the NumPy matrix. If you haven't seen it yet, it doesn't hurt to know in advance. For example, for a Pandas table:

Before I really understand it, almost every time I need to use drop, I have to re-query which axis corresponds to which, more than I can count. As in the example above, you can probably see that if you want to process a column, axis should be set to 1, and if you want to process a row, axis should be set to 0, right. But why? One of my favorite explanations (or how I remember this) goes like this:

Get the shape property of the Pandas datasheet object and you will get a tuple. The * elements of the tuple are the number of rows of the data table, and the second element is the number of columns of the data table. Think about the subscript of these two elements in Python. The first one is 0 and the last one is 1. Is that correct? So for the axis parameter, 0 is the number of rows in front of it, and 1 is the number of columns that follow.

06. Merge data tables with Concat, Merge and Join

If you are familiar with SQL, these concepts are a piece of cake for you. In any case, these functions are essentially just different ways to merge multiple tables. Of course, it's not easy to always remember which function to use under what circumstances, so let's review it again.

Concat () can simply stack one or more tables in the direction of rows (or columns) (depending on whether the axis parameter you pass in is 0 or 1).

Merge () will align two or more tables with a column primary key with the same name specified by the user.

Join () is similar to merge (), except that join () is aligned by the index of the data table, not by the same column. When an index is missing from a table, the corresponding value is NaN.

If necessary, you can also refer to the Pandas official documentation for more detailed grammar rules and application examples, and be familiar with some special situations you may encounter.

07, Apply function

You can think of apply () as a map () function, but this function is built specifically for Pandas datasheets and series objects. For beginners, you can think of series objects as array objects similar to those in NumPy. It is an one-dimensional indexed data table structure.

The apply () function applies a function to each element in a row or column you specify in a data table. Isn't it convenient? Especially when you need to format or modify all the elements of a column, you don't have to cycle over and over again!

Here are a few simple examples to familiarize you with the basic grammar rules:

08. PivotTable (Pivot Tables)

* the most important thing is the PivotTable. If you know anything about Microsoft's Excel, you've probably used (or heard of) the PivotTable feature in Excel. The built-in pivot_table () function in Pandas has the same function, which helps you format a data table and output a table like an Excel worksheet. In practice, the PivotTable will group the data according to one or more keys, pass the function into the parameter aggfunc, and the data will be counted according to the function you specify, and the result will be assigned to the table.

Here are some examples of the application of pivot_table ():

At this point, the study on "what details should be paid attention to in the process of learning Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report