Home > Net >  How to split a 1D numpy array in several horizontal arrays?
How to split a 1D numpy array in several horizontal arrays?

Time:10-30

I'm just a Python beginner and I wanted to create a graphic visualisation of Tesla stock prices from a csv file.

I imported the file, created a list then a numpy array (1D)

But I'm not able to slice it in several arrays made of the columns only. I tried np.split and np.hsplit but cannot find a way to make it work.

Here is the code


with open('Tesla.csv') as f:
    mylist = [line.rstrip('\n') for line in f]

import numpy as np
np_mylist = np.array(mylist)

Here is a slice of the data :

['Date,Close/Last,Volume,Open,High,Low'
 '10/28/2021,$1077.04,27213170,$1068.305,$1081,$1054.2'
 '10/27/2021,$1037.86,38526460,$1039.66,$1070.88,$1030.78'
 '10/26/2021,$1018.43,62414970,$1024.69,$1094.94,$1001.44'
 '10/25/2021,$1024.86,62852100,$950.53,$1045.02,$944.2'
 '10/22/2021,$909.68,22880840,$895.5,$910,$890.96']

How can i get one array for date, one for close/last, etc.

Thanks in advance for your answers

Sorry to disturb with such stupid question but i'm not able to find a way myself

CodePudding user response:

Better use pandas.DataFrame and you will have resolved all problems.

import pandas as pd

df = pd.read_csv('Tesla.csv')

And now you can access every column using df[column_name]

print( df["Date"] ) 

It has also functions to remove $ and convert string to float

df['Open'] = df['Open'].str.replace('$', '').astype(float)

And now it can be ploted

import matplotlib.pyplot as plt

df.plot(x='Date', y='Open')
plt.show()

enter image description here


Full working code.

I use io only to simulate file in memory.

text = '''Date,Close/Last,Volume,Open,High,Low
10/28/2021,$1077.04,27213170,$1068.305,$1081,$1054.2
10/27/2021,$1037.86,38526460,$1039.66,$1070.88,$1030.78
10/26/2021,$1018.43,62414970,$1024.69,$1094.94,$1001.44
10/25/2021,$1024.86,62852100,$950.53,$1045.02,$944.2
10/22/2021,$909.68,22880840,$895.5,$910,$890.96'''

import pandas as pd
import matplotlib.pyplot as plt
import io

df = pd.read_csv(io.StringIO(text))

print( df['Date'] )

# remove `$` and convert `string` to `float`
for column_name in ['Close/Last', 'Open', 'High', 'Low']:
    df[column_name] = df[column_name].str.replace('$', '', regex=False).astype(float)

ax = df.plot(x='Date', y='Open')
df.plot(x='Date', y='High', ax=ax)  # plot in the same `ax`
df.plot(x='Date', y='Low', ax=ax)   # plot in the same `ax`   
plt.show()

enter image description here


You can also plot all at once

df.plot(x='Date', y=['Open', 'High', 'Low'])

or use bars instead lines for volumne

df.plot(kind='bar', x='Date', y='Volume')

CodePudding user response:

you can read the file containing the data as a pandas dataframe and then select the column you want In Python 3.X:

import pandas as pd


df = pd.read_csv(file_path)
arr = df['col_name']

and then you can use any of the pre-implemented functions in pandas or use matplotlib or any other visualization python package you prefer you can install any package by using the following command in your terminal/command prompt pip install -U python_pkg

  • Related