Python's stock data analysis

Source: Internet
Author: User
Tags data structures
first, the initial knowledge of pandas

Pandas is a very useful library based on NumPy, which has two unique basic data Structures series (one-dimensional) and dataframe (two-dimensional) that make data operations simpler. Although pandas has two data structures, it is still a library of Python, so some data types in Python are still available here, and you can also use the class to define the data type yourself.

In the field of financial data analysis, pandas plays a very important role, for example, for quantitative trading. Pandas incorporates a large number of libraries and standard data models that provide the tools needed to efficiently manipulate large datasets, as well as billions of data-processing. second, Pandas basic operation

1, the creation of series
There are three main ways to create a series: 1 to create a series through a one-dimensional array

Import NumPy as Npimport pandas as pd# create one-dimensional array a = Np.arange (d)
print (a)
s = PD. Series (a)
print (s)

The output is as follows:



2 Create a series by means of a dictionary

Import NumPy as Npimport pandas as pd# create dictionary d = {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5}
print (d)

s = PD. Series (d)
print (s)

The output is as follows:



3 Create a series through a row or a column in the Dataframe

Refer to Dataframe below for s = df3[' One ' in the third form of creation. 2, the creation of Dataframe

There are three main ways to create a dataframe: 1 To create a dataframe through a two-dimensional array

Import NumPy as Npimport pandas as pd# create two-dimensional array a = Np.array (Np.arange ()). Reshape (3,4)
print (a)

df1 = PD. Dataframe (a)
print (DF1)

The output is as follows:



2 Create a dataframe by means of a dictionary

The following two dictionaries are used to create a data box, one is a dictionary list and one is a nested dictionary.

Import NumPy as Npimport pandas as pd

D1 = {' A ': [1,2,3,4], ' B ': [5,6,7,8], ' C ': [9,10,11,12], ' d ': [14,14,15,16]}
Print (d1)

df1 = PD. Dataframe (D1)
print (DF1)

D2 = {' One ': {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4}, ' two ': {' A ': 5, ' B ': 6, ' C ': 7, ' d ': 8}, ' three ': {' A ': 9, ' B ': ten, ' C ': One, ' d ':}}
Print (d2)

DF2 = PD. Dataframe (D2)
print (DF2)

The output is as follows:



3 Create dataframe by Dataframe way

We take out the DF2 in 2 to create the DF3

DF2 = PD. Dataframe (D2) print (DF2)

df3 = df2[[' One ', ' two ']]print (df3)

s = df3[' One ']print (s)

The output is as follows:



III. processing of stock data

Next, we learn the application of pandas in dealing with stock data through examples.
We use Pandas_datareader to get Alibaba's stock data. 1) Import the following libraries:

Import pandas as Pdimport Pandas_datareader.data as web# drawing using import Matplotlib.pyplot as plt# fetching time using import datetime
2) Set the stock name and time parameter
Name = "BABA" start = Datetime.datetime (2015,1,1) end = Datetime.date.today ()
3) Get stock data
Prices = web. DataReader (name, Google, start, end)
4) View the type of prices
Print (Type (prices))

Print as follows:

<class ' Pandas.core.frame.DataFrame ' >

You can see that the data type returned is the Dataframe type. 5) View summary information of stock

Print (Prices.describe ()

Print as follows:

Open High Low close Volumecount 791.000000 791.000000 792.000000 792.000000 7.920000e+02m Ean 106.632099 107.793186 105.355164 106.614520 1.610571e+07std 38.191772 38.539981 37.719848 38.156416 9 .941683e+06min 57.300000 58.650000 57.200000 57.390000 2.457439e+06
25% 79.855000 80.945000 79.1575   79.935000 1.003487e+07
50% 91.000000 91.740000 89.925000 90.705000 1.350020e+07
75% 119.315000 120.400000 118.462500 120.205000 1.879724e+07max 204.830000 206.200000 202.800000 205.220000 9.704593e+07

and print the latest three messages.

Print (Prices.tail (3))
Open High Down close volumedate 2018-02-21 189.37 193.17 188. 188.82 22071585
2018-02-22 190.20 190.74 187.77 188.75 12282843
2018-02-23 190.18 193.40 189.95 193. 29 16937275
6) Drawing

We will draw the stock data of Alibaba according to the opening price.

Plt.plot (
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.