Extract only elements that meet specific conditions in Python

It's easy to forget, so make a note

code

import pandas as pd

data = [
  {"name": "Taro Tanaka",   "organization": "Example1", "age": 18},
  {"name": "Hanako Yamada", "organization": "Example1", "age": 20},
  {"name": "Ichiro Suzuki", "organization": "Example2", "age": 33},
  {"name": "Michiko Sato",  "organization": "Example2", "age": 50},
  ...
]

#Orthodox way
arr = [
  r for r in data
  if r["organization"] == "Example1" and r["age"] > 18
]
## sort
sorted_arr = sorted(arr, key=lambda x: x["age"])


#Use pandas
df = pd.DataFrame(data)
arr2 = df[(df["organization"] == "Example2") & (df["age"] > 18)]
## sort
sorted_arr = df[(df["organization"] == "Example2") & (df["age"] > 18)].sort_values('age')

Recommended Posts

Extract only elements that meet specific conditions in Python
[Python] Leave only the elements that start with a specific character string in the array
[python] Move files that meet the conditions
[Introduction to Python] How to delete rows that meet multiple conditions in Pandas.DataFrame
Tips: [Python] Extract only lines that do not contain a specific string
Delete vertices that meet the conditions in networkx
Getting list elements in Python
Get the number of specific elements in a python list
python web scraping-get elements in bulk
Extract text from images in Python
Delete multiple elements in python list
Extract strings from files in Python
[Python] A program that calculates the number of chocolate segments that meet the conditions
Extract lines that match the conditions from a text file with python
Extract every n elements from an array (list) in Python and Ruby
[Python] Use pandas to extract △△ that maximizes ○○
How to extract polygon area in Python
[Python] Combine all the elements in the array
Word count that counts only words that start with a capital letter in python
Use PIL in Python to extract only the data you want from Exif
One liner that outputs multiplication tables in Python
Draw contour lines that appear in textbooks (Python)
A memo that I wrote a quicksort in Python
Add recipe in Yocto (add recipe that only copies files)
Extract only the file name excluding the directory in the directory
Building an environment that uses Python in Eclipse
[Automation] Extract the table in PDF with Python
3 steps to put Python + mecab in yum only
A program that removes duplicate statements in Python
Static type checking that starts loosely in Python
Get only the subclass elements in a list
Make slackbot only react on specific channels (Python slackbot)
Extract lines containing a specific "string" in Pandas
Extract email attachments received in Thunderbird with Python
The one that displays the progress bar in Python
Randomly select elements from list (array) in python
Sort list elements in a specified order in Python
Formulas that appear in Doing Math with Python
Get only articles from web pages in Python
Generate points only in Tokyo using python + shapely
[Python] Manipulating elements in a list (array) [Sort]
Implement PRML algorithm in Python (almost Numpy only)
How to remove duplicate elements in Python3 list
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
How to extract conditions (acquire all elements of Group that satisfy the conditions) for Group by Group