0

I have a .txt file containing int, string and floats. How can I import this .txt file as a matrix while keeping strings?

Dataset contains:

16  disk    11  10.29   4.63    30.22  nan
79  table   11  20.49   60.60   20.22  nan
17  disk    11  22.17   0.71    10.37  nan

I used:

data=np.loadtxt("/home/Desktop/dataset.txt", delimiter=',') 

the result is:

items = [conv(val) for (conv, val) in zip(converters, vals)] 
ValueError: could not convert string to float: disk

In another try I used:

data = np.genfromtxt('/home/Desktop/dataset.txt', delimiter=",")

The result is:

16.0    nan 11  10.29   4.63    30.22
79.0    nan 11  20.49   60.60   20.22
17.0    nan 11  22.17   0.71    10.37
3
  • 1
    You might want to use pandas package. It supports data frames which can be a mix of different data types. Commented Oct 24, 2015 at 1:34
  • I thought you could do this by setting the dtype parameter. Commented Oct 24, 2015 at 2:28
  • Have you tried dtype=None? Commented Oct 24, 2015 at 2:36

1 Answer 1

0

There is no way to load values of different types (e.g. str and float) to numpy array. You could use read_csv function from pandas package instead:

import pandas as pd
data = pd.read_csv("/home/Desktop/dataset.txt")

Pandas will load data to a DataFrame and you will be able to access columns and row by their names. You could read more about pandas here

Sign up to request clarification or add additional context in comments.

1 Comment

"There is no way to load values of different types (e.g. str and float) to numpy array." This is not true. Mixed types like that can be read into a structured array. See, for example, this question and answer: stackoverflow.com/questions/19622997/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.