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

使用Pandas将波士顿房价TXT文件转换为CSV的技术问询

Convert Boston Housing TXT to CSV with Pandas

No problem at all—converting that space-separated TXT file to CSV using Pandas is straightforward. Here's how to do it step by step:

1. Install Pandas (if you haven't already)

First, make sure Pandas is installed on your system. Run this command in your terminal/command prompt:

pip install pandas

2. Write the conversion script

Create a Python script (or run this in a Jupyter notebook) to handle the conversion. Since your TXT file uses spaces (possibly multiple) as separators, we'll use sep='\s+' to parse it correctly. We'll also add the standard Boston Housing column names to make the CSV usable for analysis:

import pandas as pd

# Define the standard column names for the Boston Housing dataset
column_names = [
    "CRIM", "ZN", "INDUS", "CHAS", "NOX", "RM", 
    "AGE", "DIS", "RAD", "TAX", "PTRATIO", "B", 
    "LSTAT", "MEDV"
]

# Read the TXT file: handle any number of spaces as separators, add column names
df = pd.read_csv("your_boston_housing.txt", sep='\s+', names=column_names)

# Save to CSV, skip the default index column
df.to_csv("boston_housing.csv", index=False)

Quick notes:

  • Replace "your_boston_housing.txt" with the actual file path of your TXT file (e.g., "C:/data/boston.txt" or "/home/user/docs/boston.txt").
  • The sep='\s+' parameter ensures that even if there are multiple spaces between values, Pandas will split them correctly into columns.
  • Adding the column_names makes the CSV file far more useful—you'll immediately know what each column represents instead of just seeing numbered columns.

Once you run the script, you'll have a boston_housing.csv file in the same directory (or specified path) that's ready for analysis.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:35:21