Python Notes-12

In continuation from Python Notes-11 Python Notes-10 Python Notes-9 Python Notes-8 Python Notes-7 Python Notes-6 Python Notes-5 Python Notes-4 Python Notes-3 Python Notes-2 Python Notes-1

pandas

import pandas as varanasi_panda 🙂 Each time I import this library this is what comes to my mind and then the panda (cute Chinese animal) every time. FYI I am atheist.


# Import pandas as pd

import pandas as pd

# Import the cars.csv data: cars

cars = pd.read_csv(‘cars.csv’)

# Print out cars

print(cars)

dict = {“country”: [“Brazil”, “Russia”, “India”, “China”, “South Africa”],
“capital”: [“Brasilia”, “Moscow”, “New Dehli”, “Beijing”, “Pretoria”],
“area”: [8.516, 17.10, 3.286, 9.597, 1.221],
“population”: [200.4, 143.5, 1252, 1357, 52.98] }

import pandas as pd
brics = pd.DataFrame(dict)
print(brics)

# Set the index for brics
brics.index = [“BR”, “RU”, “IN”, “CH”, “SA”]

# Print out brics with new index values
print(brics)


Install pandas, lxml .. etc
pip3 install pandas lxml html5lib BeautifulSoup4

Let us read tata motors historical data from yahoo finance

In yahoo finance in sidebar you can find the link for historical prices for any stock.
For instance go to this link

https://in.finance.yahoo.com/q/hp?s=TATAMTRDVR.BO

Now in pandas you can directly read this html file and get the tabular data.

url = 'https://in.finance.yahoo.com/q/hp?s=TATAMTRDVR.BO'
dfs = pd.read_html(url)

dfs #check what is there in the data

das[10] for me gave the actual table I want. Anyway this is not the proper way to do this as this reads many tables from the web page. Anyway for the sake of understanding we do some analysis on das[10]

df = dfs[10]

df.columns = df.iloc[0]
df.reindex(df.index.drop(0))

df.describe()

numpy

Numpy arrays are great alternatives to Python Lists. Some of the key advantages of Numpy arrays are that they are fast, easy to work with, and give users the opportunity to perform calculations across entire arrays.

In the following example, you will first create two Python lists. Then, you will import the numpy package and create numpy arrays out of the newly created lists.

# Create 2 new lists height and weight
height = [1.87, 1.87, 1.82, 1.91, 1.90, 1.85]
weight = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45]

# Import the numpy package as np
import numpy as np

# Create 2 numpy arrays from height and weight
np_height = np.array(height)
np_weight = np.array(weight)

# Calculate bmi
bmi = np_weight / np_height ** 2

# Print the result
print(bmi)

# For a boolean response
bmi > 23

# Print only those observations above 23
bmi[bmi > 23]

Some code samples for you to start

https://github.com/ndubey/algorithms/tree/master/python

I guess it is good enough python to start tacking problems. Hence

Not To be continued …

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s