It's easy to forget, so make a note
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