Python入门者求助:如何将复杂列表转换为DataFrame
Convert Your Raw List to Target DataFrame in Python
Hey there! Since you're new to Python, let's walk through exactly how to turn that unstructured list into the clean DataFrame you want. We'll cover both single-product and multi-product cases for each ID, step by step.
Step 1: Break Down the Data Structure
First, let's map out your raw list's pattern—it's grouped in sets of 3 elements:
- Positions 0, 3, 6...: Order ID (like 9308, 9306)
- Positions 1, 4, 7...: Total Value (string-formatted numbers like '127.05')
- Positions 2, 5, 8...: List of product dictionaries (each has the
product_idwe need)
Step 2: Code Implementation
First, make sure you have pandas installed (run pip install pandas in your terminal if you don't). Then use this code—it's designed to handle both single and multiple products per order:
import pandas as pd # Your raw input list (I added a multi-product example for 9308 to test the case you mentioned) raw_data = [ 9308, '127.05', [ {'id': 8568, 'name': 'some product name', 'product_id': 4204, 'variation_id': 0, 'quantity': 1, 'tax_class': '', 'subtotal': '139.00', 'subtotal_tax': '0.00', 'total': '118.15', 'total_tax': '0.00', 'taxes': [], 'meta_data': [], 'sku': '', 'price': 118.15}, {'id': 8569, 'name': 'another product', 'product_id': 4200, 'variation_id': 0, 'quantity': 1, 'tax_class': '', 'subtotal': '99.00', 'subtotal_tax': '0.00', 'total': '89.99', 'total_tax': '0.00', 'taxes': [], 'meta_data': [], 'sku': '', 'price': 89.99}, {'id': 8570, 'name': 'third product', 'product_id': 5555, 'variation_id': 0, 'quantity': 1, 'tax_class': '', 'subtotal': '100.00', 'subtotal_tax': '0.00', 'total': '90.00', 'total_tax': '0.00', 'taxes': [], 'meta_data': [], 'sku': '', 'price': 90.00} ], 9306, '98.89', [{'id': 8566, 'name': 'some product name', 'product_id': 4200, 'variation_id': 0, 'quantity': 1, 'tax_class': '', 'subtotal': '89.99', 'subtotal_tax': '0.00', 'total': '89.99', 'total_tax': '0.00', 'taxes': [], 'meta_data': [], 'sku': '', 'price': 89.99}] ] # Empty list to collect cleaned rows clean_rows = [] # Loop through the raw data in groups of 3 for i in range(0, len(raw_data), 3): order_id = raw_data[i] total_value = raw_data[i+1] products = raw_data[i+2] # For each product in the order, add a new row to our clean list for product in products: clean_rows.append({ 'ID': order_id, 'Total Value': float(total_value), # Convert string to float for numerical operations 'Product IDs': product['product_id'] }) # Convert the clean rows to a DataFrame df = pd.DataFrame(clean_rows) print(df)
Step 3: What This Code Does
- Grouping the raw data: The
range(0, len(raw_data), 3)loop steps through your list 3 elements at a time, so we always grab the right ID, total value, and product set together. - Handling multiple products: For every product in an order's product list, we create a new row with the same order ID and total value—this automatically generates the multi-row format you need for orders with multiple products.
- Data type cleanup: Converting the total value from a string to a float lets you do things like calculate sums or averages later if you need to.
Sample Output
For the multi-product example I added, your DataFrame will look exactly like what you requested:
| ID | Total Value | Product IDs | |
|---|---|---|---|
| 0 | 9308 | 127.05 | 4204 |
| 1 | 9308 | 127.05 | 4200 |
| 2 | 9308 | 127.05 | 5555 |
| 3 | 9306 | 98.89 | 4200 |
Quick Tips for Beginners
- If your raw list ever has missing elements or inconsistent grouping, you can add simple error checks (like verifying
i+1andi+2are within the list length) to avoid crashes. - You can save the final DataFrame to a CSV file with
df.to_csv('order_summary.csv', index=False)if you need to export it.
内容的提问来源于stack exchange,提问作者kubri81
相关产品推荐
相关产品推荐

