# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: PyCharm (Data-preparation)
#     language: python
#     name: pycharm-eaa1fcfe
# ---

# # Import libraries

import pandas as pd

# # Ex.1

# + [markdown] pycharm={"name": "#%% md\n"}
# a) Read in the aapl.csv, amzn.csv, fb.csv, goog.csv, and nflx.csv files.

# + pycharm={"name": "#%%\n"}
fb = pd.read_csv('C:/Users/toduc/OneDrive - National Economics University/Desktop/NEU/Machine Learning/Visualization/Data/fb.csv', sep=',')
fb

# + pycharm={"name": "#%%\n"}
aapl = pd.read_csv('C:/Users/toduc/OneDrive - National Economics University/Desktop/NEU/Machine Learning/Visualization/Data/aapl.csv', sep=',')
aapl

# + pycharm={"name": "#%%\n"}
amzn = pd.read_csv('C:/Users/toduc/OneDrive - National Economics University/Desktop/NEU/Machine Learning/Visualization/Data/amzn.csv', sep=',')
amzn

# + pycharm={"name": "#%%\n"}
goog = pd.read_csv('C:/Users/toduc/OneDrive - National Economics University/Desktop/NEU/Machine Learning/Visualization/Data/goog.csv', sep=',')
goog

# + pycharm={"name": "#%%\n"}
nflx = pd.read_csv('C:/Users/toduc/OneDrive - National Economics University/Desktop/NEU/Machine Learning/Visualization/Data/nflx.csv', sep=',')
nflx
# -

# b) Add a column to each dataframe, called ticker, indicating the ticker symbol it is for (Apple's is AAPL, for example);
# this is how you look up a stock. In this case, the filenames happen to be the ticker symbols.

# + pycharm={"name": "#%%\n"}
fb['ticker'] = 'fb'
fb

# + pycharm={"name": "#%%\n"}
aapl['ticker'] = 'aapl'
aapl

# + pycharm={"name": "#%%\n"}
amzn['ticker'] = 'amzn'
amzn

# + pycharm={"name": "#%%\n"}
goog['ticker'] = 'goog'
goog

# + pycharm={"name": "#%%\n"}
nflx['ticker'] = 'nflx'
nflx
# -

# c) Append them together into a single dataframe. (concat)

# + pycharm={"name": "#%%\n"}
faang = pd.concat([fb,aapl,amzn,nflx,goog])
faang
# -

# d) Save the result in a CSV file called faang.csv. (to_csv)

# + pycharm={"name": "#%%\n"}
faang.to_csv('faang.csv', index=True)

# + pycharm={"name": "#%%\n"}
faang = pd.read_csv('faang.csv')
faang = faang.drop(['Unnamed: 0'],axis=1)
faang
# -

# e) With faang, use type conversion to cast the values of the date column into datetimes and the volume column into
# integers. Then, sort by date and ticker.

# + pycharm={"name": "#%%\n"}
faang.assign(
    date = lambda x:pd.to_datetime(x.date),
    volumn = lambda x: x['volume'].astype('int')
)
faang

# + pycharm={"name": "#%%\n"}
faang.sort_values(by=['date', 'ticker'])
faang
# -

# f) Find the seven rows in faangwith the lowest value for volume

# + pycharm={"name": "#%%\n"}
faang.sort_values(by='volume', ascending=True)
faang.head(7)
# -

# # Ex. 2

# + [markdown] pycharm={"name": "#%% md\n"}
# a) Read in the covid19_cases.csv file.

# + pycharm={"name": "#%%\n"}
covid = pd.read_csv('C:/Users/toduc/OneDrive - National Economics University/Desktop/NEU/Machine Learning/Visualization/Data/covid19_cases.csv', sep=',')
covid
# -

# b) Create a date column using the data in the dateRep column and the pd.to_datetime() function.

# + pycharm={"name": "#%%\n"}
covid['date'] = pd.to_datetime(covid['dateRep'])
covid.head(15)
# -

# c) Set the date column as the index and sort the index.

# + pycharm={"name": "#%%\n"}
covid.sort_values(by='date', ascending=True)
# -

# d) Replace all occurrences of United_States_of_Americaand United_ Kingdom with USA and UK, respectively. Hint: the replace() method can be run on the dataframeas a whole.

# + pycharm={"name": "#%%\n"}
covid['countriesAndTerritories'].replace({'United_States_of_America':'USA', 'United_Kingdom':'UK'}, inplace=True)
# -

# e) Using the countriesAndTerritoriescolumn, filter the cleaned COVID-19 cases data down to Argentina, Brazil, China, Colombia, India, Italy, Mexico, Peru, Russia, Spain, Turkey, the UK, and the USA.

# + pycharm={"name": "#%%\n"}
covid[covid['countriesAndTerritories'].isin(['Argentina', 'Brazil', 'China', 'Colombia', 'India', 'Italy', 'Mexico'
    , 'Peru', 'Russia', 'Spain', 'Turkey', 'UK', 'USA'])]

# + [markdown] pycharm={"name": "#%% md\n"}
# f) Pivot the data so that the index contains the dates, the columns contain the country names, and the values are the case
#  counts (the cases column). Be sure to fill in NaNvalues with 0.

# + pycharm={"name": "#%%\n"}
pivot = covid.pivot(index = 'date', columns='countriesAndTerritories', values='cases')
pivot



StarcoderdataJupyterScriptsDedupFiltered