Dancia Dancia - 20 days ago 5
Python Question

Trying to append arrays

I have this piece of code:

csvData = np.array([]);
csvData = np.append(csvData, ['1', '2016-01-01', 'Some text'])
csvData = np.append(csvData, ['2', '2016-01-02', 'Some more text'])
print(csvData)


it outputs:
['1' '2016-01-01' 'Some text' '2' '2016-01-02' 'Some more text']


I would like to get something like:
[['1' '2016-01-01' 'Some text'], ['2' '2016-01-02' 'Some more text']]


I tried wrapping my data string into
[]
. The reason I am doing this, because I want to collect each csv row, sort it by specific column and iterate data rows.

Any other solution is welcome.

I would sort using something like:
csvData [np.argsort(csvData [:,2])]

Answer

Your desired output is a list of list which can be generated by the below code:

csvData = []
csvData.append(['1', '2016-01-01', 'Some text'])
csvData.append(['2', '2016-01-02', 'Some more text'])
#csvData would be
#[['1', '2016-01-01', 'Some text'], ['2', '2016-01-02', 'Some more text']]

But since you use numpy library, for converting it to the appropriate type this line helps:

csvData = np.asarray(csvData)
#csvData would be
[['1' '2016-01-01' 'Some text']
 ['2' '2016-01-02' 'Some more text']]