Numpy - Load Csv With First Row As Names Immediately To A Structured Array?
Is there a way to avoid having to predefine the names of column headers in numpy/pandas to create a structured array, and instead have numpy/pandas read in the first row as the hea
Solution 1:
For the sake of completeness, a numpy example:
rec_arr = np.genfromtxt("try.csv", delimiter = " ", names=True, dtype=None)
rec_arr
array([(b'2015-08-08', 266., 280.04, 266.82),
(b'2015-07-08', 233., 280.04, 266.82)],
dtype=[('Date', 'S10'), ('low', '<f8'), ('open', '<f8'), ('close', '<f8')])
Then, you can access columns like:
rec_arr['close']
array([ 266.82, 266.82])
and do some math as usual:
rec_arr['close'].mean()
266.81999999999999
Solution 2:
If no parameter names
in read_csv
then first row data create columns of df
.
So work:
df = pd.read_csv('file.csv')
#if necessary change default parameter sep=','
df = pd.read_csv('file.csv', sep=';')
print (df)
Date low open close
0 2015-08-08 266.0 280.04 266.82
1 2015-07-08 233.0 280.04 266.82
print (df.columns)
Index(['Date', 'low', 'open', 'close'], dtype='object')
Post a Comment for "Numpy - Load Csv With First Row As Names Immediately To A Structured Array?"