make ndarray containg string and int

398
September 12, 2017, at 4:12 PM

How to make ndarray by reading from a csv file like

12,employed,32,happy,1
21,unemployed,31,poor,0
34,rich,45,unhapppy,0

NOTE: file can be very large

output array:

[[12,"employed",32,"happy",1]
 [21,"unemployed",31,"poor",0]
 [34,"rich",45,"unhapppy",0]]

while reading from csv file using np.genfromtxt(filename,delimiter = ",",dtype = None) it make the 1-d array of tuples and with dtype = int it make strings a Nan

Answer 1

Use read_csv first and then DataFrame.values for convert to numpy array:

import pandas as pd 
df = pd.read_csv('file', header=None)
print(df)
    0           1   2         3  4
0  12    employed  32     happy  1
1  21  unemployed  31      poor  0
2  34        rich  45  unhapppy  0
arr = df.values
print(arr)
[[12 'employed' 32 'happy' 1]
 [21 'unemployed' 31 'poor' 0]
 [34 'rich' 45 'unhapppy' 0]]
Rent Charter Buses Company
READ ALSO
os.path.exists is not working as expected in python

os.path.exists is not working as expected in python

I am trying to create a directory in the home path and re-check if the directory exists in the home path before re-creating using ospath

305
Python log file Datestamp

Python log file Datestamp

I have a program that insets a CSV file into a mysql database using PythonOnce the rows / columns of the CSV file have been inserted, the Python program creates a log file, calculates the number of rows and columns contained in the CSV file and logs this data

453
Python | SKlearn | PCA

Python | SKlearn | PCA

Edit: Thanks for spotting the typo, it should be 60*50, i have corrected the same in the question

525
Perform conditional filtering based on grouped column value in Pandas Python

Perform conditional filtering based on grouped column value in Pandas Python

I want to perform filtering on Sales column such that for any Make-Auction group there should be atleast one sales is >= 100So for Acura, Copart has 101 sales so both rows for Acura would be expected in output

320