使用Python Pandas按最后一列数值下降条件拆分CSV文件
Alright, let's work through this problem together. From what I can tell, you've got a CSV-like dataset structured in groups of three values, and you want to split it into separate files whenever the numeric value in the third position drops compared to the previous row's third value. The end goal is three separate files matching your example—let's make that happen.
Step 1: Setup and Data Preparation
First, we'll use pandas since it's perfect for this kind of DataFrame manipulation. Let's start with the setup:
import pandas as pd
Next, let's get your raw data into a proper DataFrame. Your input is a flat list, so we'll split it into rows of three elements each:
# Your raw input data raw_data = ['-', 'd', '5', '-', 'd', '9', '-', 'v', '15', '-', 's', '8', '-', 's', '10', '-', 'q', '3'] # Reshape into rows with 3 columns df = pd.DataFrame([raw_data[i:i+3] for i in range(0, len(raw_data), 3)], columns=['col1', 'col2', 'col3']) # Convert the third column to numeric so we can compare values df['col3'] = pd.to_numeric(df['col3'])
Step 2: Identify Split Points
Now we need to figure out where to split the DataFrame. We'll create a flag that marks when the current row's third value is less than the previous one, then use that to assign group IDs to each segment:
# Flag rows where col3 drops from the previous row df['is_drop'] = df['col3'] < df['col3'].shift(1) # The first row has no previous value, so set its flag to False df['is_drop'].iloc[0] = False # Assign a unique group number to each segment—each drop starts a new group df['group_id'] = df['is_drop'].cumsum()
Step 3: Split and Save Segments
Finally, we'll loop through each group, strip out our helper columns, and save each segment to its own file:
# Iterate over each group and save to CSV for group_num, segment in df.groupby('group_id'): # Remove the helper columns we added output = segment.drop(columns=['is_drop', 'group_id']) # Save with 1-based file numbering (matches your expected output names) output.to_csv(f'文件{group_num + 1}.csv', index=False, header=False)
Verification
Let's confirm this produces your expected output:
- The first group includes rows with values
5,9,15(all increasing, no drop) → saved as文件1.csv - The second group starts when we drop to
8from15, then includes8and10→ saved as文件2.csv - The third group starts when we drop to
3from10→ saved as文件3.csv
Note for Existing CSV Files
If your data is already in an existing CSV file instead of a raw list, replace the data preparation step with:
df = pd.read_csv('your_input_file.csv', header=None, names=['col1', 'col2', 'col3']) df['col3'] = pd.to_numeric(df['col3'])
Adjust the header parameter or column names if your CSV has actual headers. If there's a chance of non-numeric values in the third column, add errors='coerce' to pd.to_numeric to handle that gracefully.
内容的提问来源于stack exchange,提问作者lima0

