Can i upload a table to an array in Python Numpy?
Question:
i have a table that looks like this
and i want to upload it to an array so the first colum would be city name and the first row will be the parameters.
is it possible given different types?
i need an array like this :
[[city name total 00-04 05-09]
[j 882806 110386 98268]
[a 221560 21982 19317]
[h 279591 21069 18200]]
when i use
csv = np.genfromtxt('populationData2016.csv',delimiter=",")
i get this
[[ nan nan nan ..., nan
nan nan]
[ nan 8.82806000e+05 1.10386000e+05 ..., 2.57210000e+04
1.77910000e+04 3.56080000e+04]
Answers:
You can use pandas to simplify this task:
import pandas as pd
import numpy as np
df = pd.read_csv('path/to/file.csv')
headers = np.array(df.columns) # get headers
values = df.values # numpy array of values
matrix = np.concatenate([[headers], values]) # append to the final matrix
def load_table_as_array(filename):
f = open(filename,'r')
# Parsing the first line containing column headers
header_line = f.readline()
header_line = header_line.strip(',n')
column_names = header_line.split(',’)
#Parsing the rest of the file
mat = []
row_names = []
for line in f:
tokens = line.rstrip().split(',')
row_names.append(tokens[0]) #Add first token to row header list
values = [float(n) for n in tokens[1:]] # Convert to float
mat.append(values) # Append the current row to the matrix
f.close()
row_names = np.array(row_names)
column_names = np.array(column_names)
data = np.array(mat)
return data, column_names, row_names
this works thank for who ever tried to help
To convert Pandas Dataframe to Numpy Array:
#import required model
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
#split training and testing model
x_train,x_test,y_train,y_test = train_test_split(input,output,test_size=.2)
x_train
#convert data table into numpy array
x = x_train.to_numpy()
x
i have a table that looks like this
and i want to upload it to an array so the first colum would be city name and the first row will be the parameters.
is it possible given different types?
i need an array like this :
[[city name total 00-04 05-09]
[j 882806 110386 98268]
[a 221560 21982 19317]
[h 279591 21069 18200]]
when i use
csv = np.genfromtxt('populationData2016.csv',delimiter=",")
i get this
[[ nan nan nan ..., nan
nan nan]
[ nan 8.82806000e+05 1.10386000e+05 ..., 2.57210000e+04
1.77910000e+04 3.56080000e+04]
You can use pandas to simplify this task:
import pandas as pd
import numpy as np
df = pd.read_csv('path/to/file.csv')
headers = np.array(df.columns) # get headers
values = df.values # numpy array of values
matrix = np.concatenate([[headers], values]) # append to the final matrix
def load_table_as_array(filename):
f = open(filename,'r')
# Parsing the first line containing column headers
header_line = f.readline()
header_line = header_line.strip(',n')
column_names = header_line.split(',’)
#Parsing the rest of the file
mat = []
row_names = []
for line in f:
tokens = line.rstrip().split(',')
row_names.append(tokens[0]) #Add first token to row header list
values = [float(n) for n in tokens[1:]] # Convert to float
mat.append(values) # Append the current row to the matrix
f.close()
row_names = np.array(row_names)
column_names = np.array(column_names)
data = np.array(mat)
return data, column_names, row_names
this works thank for who ever tried to help
To convert Pandas Dataframe to Numpy Array:
#import required model
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
#split training and testing model
x_train,x_test,y_train,y_test = train_test_split(input,output,test_size=.2)
x_train
#convert data table into numpy array
x = x_train.to_numpy()
x