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
approxiblue
  • 6,982
  • 16
  • 51
  • 59
Talia
  • 2,947
  • 4
  • 17
  • 28

1 Answers1

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

alechkai
  • 41
  • 3
  • 2
    *"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: http://stackoverflow.com/questions/19622997/python-genfromtext-multiple-datatypes – Warren Weckesser Oct 24 '15 at 01:53