基于CSV数据,在Pandas中筛选DataFrame30-300范围值至另一DataFrame
Got it, let's work through this problem step by step using pandas. The goal is to scan each row of your CSV, pull out values from numeric columns (mean, min, max, std) that fall between 30 and 300, then organize those values into a new DataFrame with their original column names. We'll also handle edge cases like inf and missing values to avoid errors.
Here's the complete implementation:
Step 1: Import Libraries and Load Data
First, we'll import pandas and numpy (to handle infinite values), then load your CSV file:
import pandas as pd import numpy as np # Load the CSV into a DataFrame df = pd.read_csv('your_data.csv') # Replace infinite values and empty cells with NaN (to prevent filtering issues) df = df.replace([np.inf, -np.inf], np.nan)
Step 2: Define a Row-Wise Filter Function
We'll create a function that processes each row, checks the numeric columns, and returns only the values that meet our 30-300 criteria:
def filter_row_values(row): # Specify which columns are numeric (we'll skip 'date' and 'metric') numeric_columns = ['mean', 'min', 'max', 'std'] # Use a dictionary comprehension to filter values in the target range filtered_entries = { col: val for col, val in row[numeric_columns].items() if pd.notna(val) and 30 < val < 300 } # Return as a Series to align with DataFrame columns automatically return pd.Series(filtered_entries)
Step 3: Apply the Filter and Build the Final DataFrame
Now we'll apply the function to every row, then combine the filtered results with the original date and metric columns to keep context:
# Apply the filter to each row filtered_values_df = df.apply(filter_row_values, axis=1) # Merge with original date/metric columns to maintain context for each entry final_df = pd.concat([df[['date', 'metric']], filtered_values_df], axis=1)
Step 4: Check the Result
If you print final_df, you'll get a clean DataFrame where each row contains only the values from the original numeric columns that fall in the 30-300 range. Here's what it looks like:
| date | metric | max | min | std | mean |
|---|---|---|---|---|---|
| 2018-03-15 | cpu | 34.0 | NaN | NaN | NaN |
| 2018-03-16 | mem | 40.0 | 90.0 | NaN | NaN |
| 2018-03-17 | cpu | NaN | NaN | 143.22 | NaN |
| 2018-03-18 | cpu | NaN | NaN | NaN | 52.86 |
| 2018-03-20 | mem | 67.96 | 45.33 | NaN | NaN |
| 2018-03-22 | cpu | NaN | NaN | 119.05 | NaN |
Key Notes:
- We convert
infand missing values toNaNso they don't break our range check. - The final DataFrame keeps all original numeric column names; cells where no value met the criteria are filled with
NaN. - Retaining
dateandmetricensures you know exactly which measurement each filtered value belongs to.
内容的提问来源于stack exchange,提问作者Souvik Ray

