load txt file containing string in python as matrix
Question: 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
    


Answer: There is no way to load values of different types (e.g. str and float) to
numpy array. You could use [read_csv](http://pandas.pydata.org/pandas-
docs/stable/generated/pandas.read_csv.html) 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](http://pandas.pydata.org/pandas-docs/stable/index.html)

