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

The method of python data processing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "the method of python data processing". 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 take you to learn "the method of python data processing".

Df.query filter data

Version 0.25 begins to support the query method, and its readability has been greatly improved, which is similar to the way sql query data is written, and is more user-friendly.

The following examples show that there are three common ways to filter data.

Df = pd.DataFrame ({'A':np.random.randint (1) 20, (8,))

'B':np.random.randint (1Jing 20, (8,))

'C':np.random.randint (1Jing 20, (8,))})

Df

Filter criteria: rows with column A values greater than their average and column B values greater than 5

Method 1

Df [(df ['A'] > df ["A"] .mean ()) & (df ['B'] > 5)]

Note that you must add a pair before and after ()

Method 2

Cr1 = df ['A'] > df ["A"] .mean ()

Cr2 = df ['B'] > 5

Df [cr1 & cr2]

Method 3

Mean = df ["A"] .mean ()

Df.query ("A > @ mean & B > 5")

Query is highlighted today, using syntax rules, see above, adding @ before the variable. What do you think of the readability of this grammar?

By the way, if the column name has a space in the middle, there is a special syntax to mark the column name with a pair of ``symbols:

Df = pd.DataFrame ({'First Name': [' Jack','Mary','Mike']})

Df.query ('`First Name` = =\' Jack\')

The results of the above three methods are all as follows:

Quickly find out the most categories

First read in the data:

Df = pd.read_csv ("IMDB-Movie-Data.csv")

Df

1000 rows of data. The frequency statistics of the values of genre are as follows:

Vc = df ["genre"] .value_counts ()

Vc

Print the results:

Action,Adventure,Sci-Fi 50

Drama 48

Comedy,Drama,Romance 35

Comedy 32

Drama,Romance 31

..

Adventure,Comedy,Fantasy 1

Biography,History,Thriller 1

Action,Horror 1

Mystery,Thriller,Western 1

Animation,Fantasy 1

Name: genre, Length: 207, dtype: int64

Filter out the index of top3:

Top_genre = vc [0:3] .index

Print (top_genre)

Print the results:

Index (['Action,Adventure,Sci-Fi',' Drama', 'Comedy,Drama,Romance'], dtype='object')

Use the index of the obtained top3, combined with isin, to select the corresponding df

Df_top = df [df ["genre"] .isin (top_genre)]

Df_top

Results:

At this point, I believe that you have a deeper understanding of "python data processing methods", you might as well come to the actual operation of it! 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.

Share To

Internet Technology

Wechat

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

12
Report