DataFrame列表批量处理及for循环应用疑问:时间切片失败与时间同步需求
Hey there! Let's break down your questions and fix those code issues step by step.
1. Is using a list of DataFrames a common practice?
Absolutely! Using a list to hold multiple DataFrames is totally standard when processing batch data (like your CSV files). It’s a smart way to avoid repetitive copy-pasted code, which is exactly what you’re aiming for.
You might not find tons of dedicated tutorials because sometimes folks use pd.concat() to merge all DataFrames into one early on—but if you need to process each DataFrame individually first (like calculating gradients, slicing time ranges), a list is the perfect tool for the job.
2. Fixing the time slicing loop
The issue with your current slicing code has two easy fixes:
- You’re referencing
df(the last DataFrame you read) instead of the current DataFrame in your loop - You’re creating a sliced subset but not saving it back to your list
Here’s the corrected code with clearer variable names (I renamed index to current_df to avoid confusion with the DataFrame’s index attribute):
# Option 1: Use enumerate to track positions and update the list for idx, current_df in enumerate(li): mask = (current_df.index > A_start_01) & (current_df.index <= D_end_01) # Save the sliced DataFrame back to the list li[idx] = current_df.loc[mask] # Option 2: Loop by index (if you prefer this style) for i in range(len(li)): mask = (li[i].index > A_start_01) & (li[i].index <= D_end_01) li[i] = li[i].loc[mask]
This will actually modify the DataFrames in your list to only include rows within your desired time range.
3. Syncing datetime with the first DataFrame
Since all your DataFrames have a fixed 25-second sampling interval, you can easily align them to the datetime index of the first DataFrame using reindex(). Here’s how:
# Get the datetime index from the first DataFrame as your baseline baseline_index = li[0].index # Align all other DataFrames to this baseline for i in range(1, len(li)): # Reindex to match the first DataFrame's timestamps # Use dropna() if you want to keep only matching timestamps, or ffill()/bfill() to fill missing values li[i] = li[i].reindex(baseline_index).dropna()
If you need to fill missing values instead of dropping them, replace .dropna() with .ffill() (forward fill with the last valid value) or .bfill() (backward fill with the next valid value) depending on your data needs.
Quick Optimization Note
I noticed your gradient calculation line uses x_axis three times—was that a typo? If you meant to calculate the sum of gradients for x, y, and z axes, here’s the corrected line:
for current_df in li: current_df['gradient'] = (np.abs(np.gradient(current_df['x_axis'])) + np.abs(np.gradient(current_df['y_axis'])) + np.abs(np.gradient(current_df['z_axis'])))
内容的提问来源于stack exchange,提问作者SimonDL

