Skip to content

Python Pandas: How to Convert CSV Columns to Text

Learn how to extract and convert individual or multiple CSV columns to formatted text using pandas, including handling separators, numeric data, and large files efficiently.

Converting CSV column data to text format is a common task when you need to generate reports, create readable summaries, prepare data for text-based processing, or export specific columns as plain text. Python provides several approaches to extract and convert CSV column data into text strings.

In this guide, you'll learn how to convert individual CSV columns and entire CSV files to text using pandas and built-in Python methods.

Sample CSV File

For the examples in this guide, we'll use a CSV file called fruits.csv with the following content:

FRUIT_NAME,PRICE,CATEGORY
Apple,100,Tropical
Banana,70,Tropical
JackFruit,30,Exotic
Orange,120,Citrus
Pineapple,90,Tropical
Guava,50,Tropical
Grapes,80,Berry
Mango,200,Tropical

Converting a Single Column to Text Using Pandas

The most common approach is to read the CSV with pandas, extract a column, and join its values into a text string.

import pandas as pd

df = pd.read_csv('fruits.csv')

fruit_names = ' '.join(str(item) for item in df['FRUIT_NAME'])
print("Fruit names:")
print(fruit_names)

Output:

Fruit names:
Apple Banana JackFruit Orange Pineapple Guava Grapes Mango

Using Different Separators

You can join values with any separator - commas, newlines, pipes, or custom delimiters:

import pandas as pd

df = pd.read_csv('fruits.csv')

prices_newline = '\n'.join(str(p) for p in df['PRICE'])
print("Prices (one per line):")
print(prices_newline)

print()

fruits_comma = ', '.join(df['FRUIT_NAME'])
print(f"Fruits: {fruits_comma}")

fruits_pipe = ' | '.join(df['FRUIT_NAME'])
print(f"Fruits: {fruits_pipe}")

Output:

Prices (one per line):
100
70
30
120
90
50
80
200

Fruits: Apple, Banana, JackFruit, Orange, Pineapple, Guava, Grapes, Mango
Fruits: Apple | Banana | JackFruit | Orange | Pineapple | Guava | Grapes | Mango

Using map() for Type Conversion

When working with numeric columns, use map(str, ...) to convert all values to strings before joining:

import pandas as pd

df = pd.read_csv('fruits.csv')

price_text = ', '.join(map(str, df['PRICE']))
print(f"Prices: {price_text}")

Output:

Prices: 100, 70, 30, 120, 90, 50, 80, 200

Converting Multiple Columns to Text

Specific Columns Side by Side

import pandas as pd

df = pd.read_csv('fruits.csv')

lines = [f"{name}: ${price}" for name, price in zip(df['FRUIT_NAME'], df['PRICE'])]
text = '\n'.join(lines)

print(text)

Output:

Apple: $100
Banana: $70
JackFruit: $30
Orange: $120
Pineapple: $90
Guava: $50
Grapes: $80
Mango: $200

All Columns as Formatted Text

import pandas as pd

df = pd.read_csv('fruits.csv')

text = df.to_string(index=False)
print(text)

Output:

FRUIT_NAME  PRICE CATEGORY
     Apple    100 Tropical
    Banana     70 Tropical
 JackFruit     30   Exotic
    Orange    120   Citrus
 Pineapple     90 Tropical
     Guava     50 Tropical
    Grapes     80    Berry
     Mango    200 Tropical

Converting an Entire CSV File to Text (Without Pandas)

For a lightweight approach that doesn't require pandas, read the CSV file directly and process it as text:

with open('fruits.csv', 'r') as f:
    content = f.read()

text = content.replace(',', ' ')

print(text)

Output:

FRUIT_NAME PRICE CATEGORY
Apple 100 Tropical
Banana 70 Tropical
JackFruit 30 Exotic
Orange 120 Citrus
Pineapple 90 Tropical
Guava 50 Tropical
Grapes 80 Berry
Mango 200 Tropical

Using the csv Module for More Control

import csv

with open('fruits.csv', 'r') as f:
    reader = csv.reader(f)

    next(reader)

    lines = []
    for row in reader:
        lines.append(f"{row[0]} costs {row[1]}")

text = '\n'.join(lines)
print(text)

Output:

Apple costs 100
Banana costs 70
JackFruit costs 30
Orange costs 120
Pineapple costs 90
Guava costs 50
Grapes costs 80
Mango costs 200

Saving Converted Text to a File

After converting CSV data to text, you'll often want to save it:

import pandas as pd

df = pd.read_csv('fruits.csv')

lines = [f"{name} - ${price} ({cat})"
         for name, price, cat in zip(df['FRUIT_NAME'], df['PRICE'], df['CATEGORY'])]
text = '\n'.join(lines)

with open('fruits.txt', 'w') as f:
    f.write(text)

print("Saved to fruits.txt")
print(text)

Output:

Saved to fruits.txt
Apple - $100 (Tropical)
Banana - $70 (Tropical)
JackFruit - $30 (Exotic)
Orange - $120 (Citrus)
Pineapple - $90 (Tropical)
Guava - $50 (Tropical)
Grapes - $80 (Berry)
Mango - $200 (Tropical)

Handling Large CSV Files

Practical Example: Generating a Summary Report

import pandas as pd

df = pd.read_csv('fruits.csv')

report_lines = [
    "=" * 40,
    "FRUIT INVENTORY REPORT",
    "=" * 40,
    "",
    f"Total items: {len(df)}",
    f"Price range: ${df['PRICE'].min()} - ${df['PRICE'].max()}",
    f"Average price: ${df['PRICE'].mean():.0f}",
    "",
    "Items by category:",
]

for category, group in df.groupby('CATEGORY'):
    items = ', '.join(group['FRUIT_NAME'])
    report_lines.append(f"  {category}: {items}")

report = '\n'.join(report_lines)
print(report)

Output:

========================================
FRUIT INVENTORY REPORT
========================================

Total items: 8
Price range: $30 - $200
Average price: $92

Items by category:
  Berry: Grapes
  Citrus: Orange
  Exotic: JackFruit
  Tropical: Apple, Banana, Pineapple, Guava, Mango

Quick Comparison of Methods

Method Requires Pandas Column Selection Memory Efficient Best For
pandas + join() ✅ ✅ Easy 🔶 Specific columns, data analysis
df.to_string() ✅ ✅ 🔶 Quick formatted output
Built-in open() + replace() ❌ ❌ (whole file) ✅ Simple full-file conversion
csv module ❌ ✅ ✅ Row-by-row processing

Conclusion

Converting CSV columns to text in Python can be done in several ways depending on your needs:

  • Use pandas with join() for the most flexible approach - it lets you select specific columns, apply formatting, and use any separator.
  • Use df.to_string() for a quick, formatted text representation of the entire DataFrame.
  • Use built-in file reading with replace() for a lightweight, no-dependency conversion of the entire CSV file.
  • Use the csv module for row-by-row processing that's memory-efficient for large files.

For most use cases, the pandas approach with ' '.join(str(x) for x in df['column']) provides the best balance of simplicity, flexibility, and readability.

Navigation