In [2]:
## https://chrisalbon.com/python/basics/strings_to_datetime/
#Step 1:
#Write a program to download stock data by calling this URL and save it inside a pandas dataframe
#https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=8WAC2EQWT6CHVMKY&datatype=csv
## Step2:
#Filter the dataframe to contain only the rows where the open price is highest open price during that week.
#A week is defined as Monday - Friday
Save this filtered dataframe to an output file.
import pandas as pd
df = pd.read_csv("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=8WAC2EQWT6CHVMKY&datatype=csv")
In [3]:
df.head()
Out[3]:
In [13]:
df.shape
Out[13]:
In [14]:
df.dtypes
Out[14]:
In [18]:
df['datetime'] = pd.to_datetime(df.timestamp)
In [19]:
df.head()
Out[19]:
In [20]:
df.dtypes
Out[20]:
In [22]:
df.drop('datetime',axis=1,inplace=True)
In [23]:
df.head()
Out[23]:
In [24]:
df['timestamp'] = pd.to_datetime(df.timestamp)
In [25]:
df.head()
Out[25]:
In [26]:
df.dtypes
Out[26]:
In [29]:
col1 = df.iloc[:,0]
In [31]:
df['weekOfYear'] = df['timestamp'].dt.weekofyear
In [32]:
df.head()
Out[32]:
In [33]:
## http://hamelg.blogspot.in/2015/11/python-for-data-analysis-part-17.html
df['year'] = df['timestamp'].dt.year
df['month'] = df['timestamp'].dt.month
In [34]:
df.head()
Out[34]:
In [46]:
dfmaxOpen = df.groupby("weekOfYear").open.max()
In [47]:
dfmaxOpen
Out[47]:
In [ ]:
## http://hamelg.blogspot.in/2015/11/python-for-data-analysis-part-18.html
In [42]:
df.describe()
Out[42]:
In [43]:
df.dtypes
Out[43]:
In [44]:
df.describe(include=['int64'])
Out[44]:
In [45]:
df.head()
Out[45]:
In [48]:
########################################################################################################
########################################################################################################
In [49]:
drinks = pd.read_csv('http://bit.ly/drinksbycountry')
In [50]:
drinks.head()
Out[50]:
In [51]:
drinks.beer_servings.mean() # accross the entire dataset
Out[51]:
In [53]:
## beer_serving mean by continent
drinks.groupby('continent').beer_servings.mean() ## Gives for each continent, the avg beer_servings
Out[53]:
In [55]:
## lets fileter the dataset by Africa
drinks[drinks.continent=='Africa'].beer_servings.mean()
Out[55]:
In [56]:
drinks[drinks.continent=='Europe'].beer_servings.mean()
Out[56]:
In [57]:
drinks.groupby('continent').beer_servings.max()
Out[57]:
In [59]:
## We can use multiple aggregate functions
drinks.groupby('continent').beer_servings.agg(['count','min','max','mean'])
## count gives the no of records in a particular continent
Out[59]:
In [60]:
drinks.groupby('continent').mean() ## mean of all numeric cols by continent
Out[60]:
In [61]:
%matplotlib inline
In [62]:
drinks.groupby('continent').mean().plot(kind='bar')
Out[62]:
In [ ]: