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

How does pandas filter data for specified rows or columns

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains the "pandas how to filter the specified row or column data", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "pandas how to filter the specified row or column data" it!

The two main data structures of pandas are series (equivalent to a row or column data mechanism) and DataFrame (equivalent to a table data mechanism with multiple rows and columns).

This article makes an associative analogy with rows or columns operated by excel or sql for ease of understanding.

1. Re-indexing: reindex and ix

As described in the previous article, the default row index after data reading is 0Pert 1, 2, and 3. Such a sequence number. The column index is equivalent to the field name (that is, the first row of data), where re-indexing means that the default index can be changed to what you want.

1.1 Series

For example, data=Series ([4Magne5pr 6], index= ['afeiretheconc']), and the row index is aPerry brecriciec.

After we modify the index with data.reindex (['axiaxiajiaozhuo']), we output:

It can be understood that after we set the index with reindex, we match the corresponding value in the original data according to the index, and the one that does not match is NaN.

1.2 DataFrame

(1) Row index modification: DataFrame row index is the same as Series

(2) column index modification: column index modification is specified by using the parameter columns, referring to reindex (columns= ['m _ 1','M _ 1,'M _ 2, M _ 2, and M _ 2). The modification logic is similar to the row index, which is also equivalent to using the new column index to match the original data without setting the NaN on the match.

Example:

(3) to modify both row and column indexes, you can use

two。 Discard the column on the specified axis (the popular saying is to delete a row or column): drop

Select which row or column to delete through the index

Data.drop (['axiomain c']) is equivalent to delete table a where xid='a' or xid='c'

Data.drop ('M1 recording journal axisym1) is equivalent to delete table a where yid='m1'.

3. Select and filter (in popular terms, sql filters queries by criteria)

Because there are row and column indexes in python, it is more convenient to filter the data.

3.1 Series

(1) Select according to the row index, as shown in

Obj ['b'] is equivalent to select * from tb where xid='b'obj, and the results are displayed in the order of b, a, c, which is the difference from sql [0:1] and obj [0:1] and the difference between obj and obj is as follows:

# the former does not contain the end, while the latter contains the end

(2) filtering obj according to the size of the value [obj >-0.6] is equivalent to finding records with a value greater than-0.6 in obj data for display.

3.2 DataFrame

(1) choose ix or xs for a single line:

For example, the row record with index b is filtered in the following three ways

(2) Select multiple lines:

How to filter two row records with an index of aformab

# the above cannot be directly written as data [['axiajiaozhongb']]

Data [0:2] represents a record from the first row to the second row. The first line starts at 0 by default and does not contain the 2 at the end.

(3) Select a single column

Filter all row record data for M1 column

(4) Select multiple columns

Filter the data recorded in two columns and all rows of m _ 1 ~ m ~ 3

The one in front of ix [:, ['m _ 1 _ blank]] indicates that all rows are filtered in.

(5) filter rows or columns according to the size of the value

If all records with a column value greater than 4 are filtered out, it is equivalent to select * from tb where column name > 4

(6) if all records with a column value greater than 4 are filtered and only some of the columns need to be shown

The row is filtered with criteria, and the column is filtered for the first and third columns with [0quotient 2]

Thank you for your reading, the above is the content of "how pandas filters the specified row or column data". After the study of this article, I believe you have a deeper understanding of how pandas filters the specified row or column data, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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