How to import txt files having continuous variables in Python?

read_write
python

#1

Hi,

I am working on UCI machine learning challenge “Human Activity Recognition Using Smartphones Data Set”. This challenge has a dataset “X_train.txt” and when I import it using read_csv it stores all variables in a single column only (look at below code).

Code

import pandas as pd
df=pd.read_csv('C:/Users/Pravin/Desktop/UCI HAR Dataset/train/X_train.txt', sep="\t", header=None)
df.head()

Output

Please help me to read file as the numbers separated by spaces should be under different columns. One of the methods I have figured out save as txt file as csv and after that import it.

Thanks,
Pravin


#2

@pravin, are you sure that the elements are separated by a single tab character?


#3

@anon,

it is tab delimited, you can see below snapshot or download the file from UCI machine learning challenge.
http://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones

Thanks,
Pravin


#4

I checked the file, it’s separated by a single whitespace at some places, and where there isn’t a minus sign, there are two whitespaces. So it’s not as consistent as you think it is.

P.S.: @Kunal, I really like this pic upload feature which copies the image directly from the clipboard. :smile: