您的当前位置:首页正文

Pandas Numpy Advanced

2024-11-06 来源:个人技术集锦

Pandas

df[('Cat','apple'), level=[0,2])
df.loc[('Cat',slice('food', 'water'), 'apple')]
  1. mask:
    In numpy:
import numpy as np

# Sample data
N = 100000
x = np.random.uniform(size=N)
y = np.random.uniform(size=N)

# Calculate z
z = np.sqrt(x**2 + y**2)

# Mask condition: keep elements where z <= 1
condition = z <= 1

# Use boolean indexing to filter elements that meet the condition
filtered_z = z[condition]

#if you want to set the element to 0 when they meet the condition
z[condition]=0
Top