Transpose CSV

Swap rows and columns in CSV files

Accepted format: CSV (Maximum size: 500 MB)

Transpose CSV

Function

Transposing a CSV file swaps rows and columns. The original rows become columns and the original columns become rows. This operation is commonly used when reshaping datasets for downstream analysis, visualization, or compatibility with specific software tools.


Input Format

  • A CSV file containing tabular data.
  • Header row is optional.
  • All rows should contain a consistent number of columns.

Output Format

  • A transposed CSV file.
  • Original rows become columns.
  • Original columns become rows.

Applications

  • Data reorganization and reshaping.
  • Sample-by-feature conversion in genomics and transcriptomics datasets.
  • Preparing files for plotting libraries.
  • Improving readability of very wide tables.
  • Meeting format requirements of downstream tools.

Example

Input:

Gene,Sample1,Sample2
BRCA1,12,18
TP53,25,30


Output:

Gene,BRCA1,TP53
Sample1,12,25
Sample2,18,30

Python Example

import pandas as pd

df = pd.read_csv("input.csv")

df.T.to_csv(
    "transposed.csv",
    header=False
)

AWK Example

awk -F',' '{
    for (i=1; i<=NF; i++)
        a[i,NR]=$i

    if (NF>p)
        p=NF
}
END {
    for (i=1; i<=p; i++) {
        for (j=1; j<=NR; j++)
            printf "%s%s",
                   a[i,j],
                   (j==NR ? RS : ",")
    }
}' input.csv > transposed.csv

Suggested Reading


Important Note: Transposing very large CSV files may require substantial memory because the entire dataset typically needs to be loaded before rows and columns can be exchanged. Large files may take longer to process.