In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use query() for elegant queries in Python". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and study and learn "how to use query() for elegant queries in Python" together!
For Pandas to obtain specified data according to conditions, I believe everyone can easily write the corresponding code, but if you haven't used query, I believe you will be impressed by its simplicity!
conventional usage
First, create a DataFrame.
import pandas as pddf = pd.DataFrame( {'A': ['e', 'd', 'c', 'b', 'a'], 'B': ['f', 'b', 'c', 'd', 'e'], 'C': range(0, 10, 2), 'D': range(10, 0, -2), 'E.E': range(10, 5, -1)})
We now select all rows where the letters in column A appear in column B. Let's look at two common ways of writing.
>>> df[df['A'].isin(df['B'])] A B C D E.E0 e f 0 10 101 d b 2 8 92 c c 4 6 83 b d 6 4 7>>> df.loc[df['A'].isin(df['B'])] A B C D E.E0 e f 0 10 101 d b 2 8 92 c c 4 6 83 b d 6 4 7
This is done using query().
>>> df.query("A in B") A B C D E.E0 e f 0 10 101 d b 2 8 92 c c 4 6 83 b d 6 4 7
You can see that the code after using query is simple and easy to understand, and it consumes less memory.
multi-conditional query
Select all rows where the letters in column A appear in column B and column C is smaller than column D.
>>> df.query('A in B and C
< D') A B C D E.E0 e f 0 10 101 d b 2 8 92 c c 4 6 8 这里 and 也可以用 & 表示。 引用变量 表达式中也可以使用外部定义的变量,在变量名前用@标明。 >>> number = 5>>> df.query('A in B & C > @number') A B C D E.E3 b d 6 4 7
index selection
Select all rows where the letters in column A appear in column B and have an index greater than 2.
>>> df.query('A in B and index > 2') A B C D E.E3 b d 6 4 7
multiple index selection
Create a DataFrame with a two-level index.
>>> import numpy as np>>> colors = ['yellow']*3 + ['red']*2>>> rank = [str(i) for i in range(5)]>>> index = pd.MultiIndex.from_arrays([colors, rank], names=['color', 'rank'])>>> df = pd.DataFrame(np.arange(10).reshape(5, 2),columns=['A', 'B'] , index=index)>>> df = pd.DataFrame(np.arange(10).reshape(5, 2),columns=['A', 'B'] , index=index)>>> df A Bcolor rank yellow 0 0 1 1 2 3 2 4 5red 3 6 7 4 8 9
1. When there is a multi-level index with a name, select it directly through the index name.
>>> df.query("color == 'red'") A Bcolor rank red 3 6 7 4 8 9
2. When there are multiple index nameless, select by index level.
>>> df.index.names = [None, None]>>> df.query("ilevel_0 == 'red'") A Bred 3 6 7 4 8 9>>> df.query("ilevel_1 == '4'") A Bred 4 8 9
special characters
For column names with spaces or operators in the middle of other special symbols, you need to use backquotes ``.
>>> df.query('A == B | (C + 2 > `E.E`)') A B C D E.E2 c c 4 6 83 b d 6 4 74 a e 8 2 6
In general, query() usage is relatively simple, can be quickly started, code readability has also improved a lot.
Thank you for reading, the above is the content of "how to use query() for elegant query in Python", after learning this article, I believe everyone has a deeper understanding of how to use query() for elegant query in Python, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.