You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中重排DataFrame:按每60列拆分宽表转长格式

Reshape Wide DataFrame to Long Format by 60-Column Chunks

Hey there! Let's figure out how to reshape your wide DataFrame (24 rows × 2820 columns) into a long format by splitting it into chunks of 60 columns and stacking them vertically. I'll use pandas since it's perfect for this kind of data reshaping, and I'll walk through a concrete example matching the 6-row, 12-column sample you mentioned.

Step 1: Core Workflow Overview

We'll split your wide table into groups of 60 columns, convert each group to a row-oriented (long) format, then stack all converted groups on top of each other. This keeps all your original data intact while restructuring it to fit a vertical layout.

Step 2: Test with Sample Data (6 Rows × 12 Columns)

First, let's simulate your sample data to validate the approach:

import pandas as pd
import numpy as np

# Simulate 6 rows × 12 columns sample data
np.random.seed(42)
df_sample = pd.DataFrame(
    np.random.randint(0, 100, size=(6, 12)),
    columns=[f"col_{i+1}" for i in range(12)]
)

Step 3: Reusable Reshaping Function

Here's a flexible function that handles chunking and reshaping. For your use case, we'll set cols_per_block=60; for the sample, we'll use 6 to split the 12 columns into 2 chunks:

def reshape_wide_to_long(df, cols_per_block=60):
    # Split columns into chunks of the specified size
    column_chunks = [df.columns[i:i+cols_per_block] for i in range(0, len(df.columns), cols_per_block)]
    
    reshaped_chunks = []
    for chunk_num, chunk_cols in enumerate(column_chunks, 1):
        # Extract the current chunk and preserve original row IDs
        chunk_df = df[chunk_cols].reset_index(names="original_row_id")
        
        # Convert chunk from wide to long format
        long_chunk = chunk_df.melt(
            id_vars="original_row_id",
            var_name="source_column",
            value_name="data_value"
        )
        
        # Add a chunk identifier to track which column group the data came from
        long_chunk["chunk_id"] = chunk_num
        reshaped_chunks.append(long_chunk)
    
    # Stack all chunks into the final long DataFrame
    return pd.concat(reshaped_chunks, ignore_index=True)

Step 4: Apply to Your Data

For the sample data (12 columns, split into 6-column chunks):

long_sample = reshape_wide_to_long(df_sample, cols_per_block=6)
print(long_sample.head())

This outputs a long DataFrame with 72 rows (one row per original cell), plus metadata columns to track the original row, source column, and chunk group.

For your 24×2820 DataFrame:

Simply call the function with the default chunk size:

# Assume your original DataFrame is named df_original
final_long_df = reshape_wide_to_long(df_original)

Key Notes

  • Preserve Row Context: The original_row_id column keeps track of which row each value came from in the wide table. If your original DataFrame has a named index, adjust reset_index(names="your_index_name") to match.
  • Chunk Tracking: The chunk_id column helps trace back which group of 60 columns a value originated from—you can remove this line if you don't need it.
  • Flexibility: This approach works regardless of your column names, unlike pd.wide_to_long which relies on structured column naming patterns.

内容的提问来源于stack exchange,提问作者Vip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:18:42