XXX
XXX

Reputation: 181

cannot separate .txt file by commas using pd.read_fwf()

I want to read the following .txt file and convert it to a pandas dataFrame object. But I cannot separate the columns. I have tried pd.read_fwf('housing.txt',delimiters=','). It didn't work.

df = pd.read_fwf('housing.txt',delimiter = ',',names=['a','b','c'])

The output:

a   b   c
0   2104,3,399900   NaN NaN
1   1600,3,329900   NaN NaN
2   2400,3,369000   NaN NaN
3   1416,2,232000   NaN NaN
4   3000,4,539900   NaN NaN
5   1985,4,299900   NaN NaN
6   1534,3,314900   NaN NaN

Here is the housing.txt file

2104,3,399900
1600,3,329900
2400,3,369000
1416,2,232000
3000,4,539900
1985,4,299900
1534,3,314900
1427,3,198999
1380,3,212000

Upvotes: 0

Views: 601

Answers (1)

RJ Adriaansen
RJ Adriaansen

Reputation: 9619

You can use read_csv, as this is a comma-separated values (CSV) file:

df = pd.read_csv("housing.txt", names=['a','b','c'])

Upvotes: 2

Related Questions