Python Pandas: How to Convert CSV Columns to Text
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
# Read the CSV file
df = pd.read_csv('fruits.csv')
# Convert the 'FRUIT_NAME' column to a space-separated string
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')
# Newline-separated
prices_newline = '\n'.join(str(p) for p in df['PRICE'])
print("Prices (one per line):")
print(prices_newline)
print()
# Comma-separated
fruits_comma = ', '.join(df['FRUIT_NAME'])
print(f"Fruits: {fruits_comma}")
# Pipe-separated
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')
# Convert numeric PRICE column to text
price_text = ', '.join(map(str, df['PRICE']))
print(f"Prices: {price_text}")
Output:
Prices: 100, 70, 30, 120, 90, 50, 80, 200
map(str, column) is slightly faster than a generator expression (str(x) for x in column) for large datasets, and both produce the same result.
Converting Multiple Columns to Text
Specific Columns Side by Side
import pandas as pd
df = pd.read_csv('fruits.csv')
# Combine two columns into formatted text
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')
# Convert entire DataFrame to a formatted string
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:
# Read the entire CSV file
with open('fruits.csv', 'r') as f:
content = f.read()
# Replace commas with spaces
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)
# Skip header
next(reader)
# Extract and format specific columns
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')
# Create formatted text
lines = [f"{name} - ${price} ({cat})"
for name, price, cat in zip(df['FRUIT_NAME'], df['PRICE'], df['CATEGORY'])]
text = '\n'.join(lines)
# Save to a text file
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
For very large CSV files, avoid loading the entire file into memory. Process and write line by line:
import csv
with open('large_data.csv', 'r') as infile, open('output.txt', 'w') as outfile:
reader = csv.DictReader(infile)
for row in reader:
line = f"{row['FRUIT_NAME']}: ${row['PRICE']}\n"
outfile.write(line)
print("Conversion complete.")
This processes one row at a time, keeping memory usage constant regardless of file size.
Practical Example: Generating a Summary Report
import pandas as pd
df = pd.read_csv('fruits.csv')
# Build a text report
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
csvmodule 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.