从列表元素的每一行创建DataFrame:假设列表内DataFrame结构一致
Got it, since you have a list of DataFrames with exactly the same structure (like your final01_1, final02_1, final03_1 examples), here are the most practical ways to create a single consolidated DataFrame from all their rows:
Method 1: Use pd.concat() (Recommended)
This is the standard, efficient way to stack all rows from your DataFrame list into one.
import pandas as pd # Assume your list of DataFrames is defined like this df_list = [final01_1, final02_1, final03_1] # Combine all DataFrames into one combined_df = pd.concat(df_list, ignore_index=True) # Preview the result print(combined_df.head())
- Key Note: The
ignore_index=Trueparameter resets the index of the final DataFrame, avoiding duplicate index values from the original individual DataFrames. - Optional: If you want to track which original DataFrame each row came from, add a source identifier column first:
# Add a 'source' column to each DataFrame in the list for index, df in enumerate(df_list, start=1): df['source'] = f'final{index:02d}_1' # Now combine them combined_df = pd.concat(df_list, ignore_index=True)
Method 2: Build from Individual Rows
If you specifically need to extract each row first before creating the final DataFrame (e.g., for custom processing per row), you can do this:
import pandas as pd df_list = [final01_1, final02_1, final03_1] all_rows = [] # Extract every row from each DataFrame as a dictionary for df in df_list: all_rows.extend(df.to_dict('records')) # Create the final DataFrame from the collected rows combined_df = pd.DataFrame(all_rows)
This works because df.to_dict('records') converts each row of the DataFrame into a dictionary (with column names as keys), and we collect all these dictionaries before building the new DataFrame.
Verification
After running either method, your combined_df will have the same column structure as your original DataFrames, and will contain all rows from every DataFrame in your list.
内容的提问来源于stack exchange,提问作者rook1996

