You are on page 1of 8

18/07/2023 18:21 Python_Exercises

Out[55]: Country OrderDate DeliverDate Item ShippedUnits

0 Belgium 2007-10-01 2007-10-04 a 10

6 France 2007-10-11 2007-10-13 a 4

11 France 2007-10-12 2007-10-14 a 3

12 France 2007-10-12 2007-10-12 a 2

14 Belgium 2007-10-13 2007-10-13 a 6

... ... ... ... ... ...

9978 Belgium 2019-05-15 2019-05-21 a 3

9984 Belgium 2019-05-15 2019-05-19 a 3

9996 Germany 2019-05-21 2019-05-22 a 4

9998 France 2019-05-22 2019-05-25 a 3

9999 France 2019-05-22 2019-05-22 a 1

2017 rows × 5 columns

In [56]: shipping_data[shipping_data['ShippedUnits']>3]

Out[56]: Country OrderDate DeliverDate Item ShippedUnits

0 Belgium 2007-10-01 2007-10-04 a 10

2 UK 2007-10-06 2007-10-15 b 10

3 Belgium 2007-10-06 2007-10-12 c 4

6 France 2007-10-11 2007-10-13 a 4

14 Belgium 2007-10-13 2007-10-13 a 6

... ... ... ... ... ...

9972 Belgium 2019-05-14 2019-05-19 b 6

9988 Belgium 2019-05-20 2019-05-21 b 10

9994 France 2019-05-21 2019-05-23 b 4

9995 UK 2019-05-21 2019-05-22 c 4

9996 Germany 2019-05-21 2019-05-22 a 4

2218 rows × 5 columns

In [57]: shipping_data[(shipping_data['ShippedUnits']>3) & (shipping_data['Country']=='Franc

file:///C:/Users/adrie/Downloads/Python_Exercises.html 7/14
18/07/2023 18:21 Python_Exercises

Out[57]: Country OrderDate DeliverDate Item ShippedUnits

6 France 2007-10-11 2007-10-13 a 4

21 France 2007-10-17 2007-10-20 a 4

36 France 2007-10-22 2007-10-24 a 6

37 France 2007-10-23 2007-10-24 b 10

59 France 2007-10-30 2007-10-30 a 4

... ... ... ... ... ...

9948 France 2019-05-05 2019-05-08 b 6

9954 France 2019-05-08 2019-05-11 b 4

9962 France 2019-05-11 2019-05-14 c 10

9967 France 2019-05-12 2019-05-14 a 4

9994 France 2019-05-21 2019-05-23 b 4

946 rows × 5 columns

In [75]: import os

In [76]: os.getcwd()

'/content'
Out[76]:

In [77]: path="sample_data/"

In [78]: path

'sample_data/'
Out[78]:

In [81]: path+'shippments.xlsx'

'sample_data/shippments.xlsx'
Out[81]:

In [82]: df1=shipping_data[shipping_data['Country']=='France']
with pd.ExcelWriter(path+'shippments.xlsx') as writer:
df1.to_excel(writer, sheet_name='France')

In [83]: df2=shipping_data[shipping_data['Country']=='Belgimu']
with pd.ExcelWriter(path+'shippments.xlsx', engine='openpyxl', mode='a') as writer
df2.to_excel(writer, sheet_name='Belgium')

In [84]: shipping_data['ShippedUnits'].plot.hist()

<Axes: ylabel='Frequency'>
Out[84]:

file:///C:/Users/adrie/Downloads/Python_Exercises.html 8/14
18/07/2023 18:21 Python_Exercises

In [85]: shipping_data['Country'].value_counts().plot(kind='bar')

<Axes: >
Out[85]:

In [ ]:

EXERCISE 4

file:///C:/Users/adrie/Downloads/Python_Exercises.html 9/14
18/07/2023 18:21 Python_Exercises

In [100… tims=shipping_data.groupby('OrderDate').sum()

tims.head()

<ipython-input-100-37a26bb9e350>:1: FutureWarning: The default value of numeric_on


ly in DataFrameGroupBy.sum is deprecated. In a future version, numeric_only will d
efault to False. Either specify numeric_only or select only columns which should b
e valid for the function.
tims=shipping_data.groupby('OrderDate').sum()
Out[100]: ShippedUnits

OrderDate

2007-10-01 10

2007-10-02 2

2007-10-06 16

2007-10-09 3

2007-10-11 13

In [101… tims.plot()
tims.cumsum().plot()

<Axes: xlabel='OrderDate'>
Out[101]:

file:///C:/Users/adrie/Downloads/Python_Exercises.html 10/14
18/07/2023 18:21 Python_Exercises

EXERCISE 5

In [102… tims= shipping_data.groupby(['OrderDate','Country']).sum()


tims.head()

<ipython-input-102-dce4bf80a674>:1: FutureWarning: The default value of numeric_on


ly in DataFrameGroupBy.sum is deprecated. In a future version, numeric_only will d
efault to False. Either specify numeric_only or select only columns which should b
e valid for the function.
tims= shipping_data.groupby(['OrderDate','Country']).sum()
Out[102]: ShippedUnits

OrderDate Country

2007-10-01 Belgium 10

2007-10-02 Germany 2

2007-10-06 Belgium 6

UK 10

2007-10-09 Germany 3

In [103… tims = tims.unstack()

In [104… tims.head()

file:///C:/Users/adrie/Downloads/Python_Exercises.html 11/14
18/07/2023 18:21 Python_Exercises

Out[104]: ShippedUnits

Country Belgium France Germany UK

OrderDate

2007-10-01 10.0 NaN NaN NaN

2007-10-02 NaN NaN 2.0 NaN

2007-10-06 6.0 NaN NaN 10.0

2007-10-09 NaN NaN 3.0 NaN

2007-10-11 3.0 10.0 NaN NaN

In [106… tims = tims.fillna(0)

In [107… tims.head()

Out[107]: ShippedUnits

Country Belgium France Germany UK

OrderDate

2007-10-01 10.0 0.0 0.0 0.0

2007-10-02 0.0 0.0 2.0 0.0

2007-10-06 6.0 0.0 0.0 10.0

2007-10-09 0.0 0.0 3.0 0.0

2007-10-11 3.0 10.0 0.0 0.0

In [108… tims.cumsum().plot()

<Axes: xlabel='OrderDate'>
Out[108]:

file:///C:/Users/adrie/Downloads/Python_Exercises.html 12/14
18/07/2023 18:21 Python_Exercises

In [ ]:

EXERCISE 6

In [109… !pip install pandas_datareader

Requirement already satisfied: pandas_datareader in /usr/local/lib/python3.10/dist


-packages (0.10.0)
Requirement already satisfied: lxml in /usr/local/lib/python3.10/dist-packages (fr
om pandas_datareader) (4.9.2)
Requirement already satisfied: pandas>=0.23 in /usr/local/lib/python3.10/dist-pack
ages (from pandas_datareader) (1.5.3)
Requirement already satisfied: requests>=2.19.0 in /usr/local/lib/python3.10/dist-
packages (from pandas_datareader) (2.27.1)
Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.1
0/dist-packages (from pandas>=0.23->pandas_datareader) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-pack
ages (from pandas>=0.23->pandas_datareader) (2022.7.1)
Requirement already satisfied: numpy>=1.21.0 in /usr/local/lib/python3.10/dist-pac
kages (from pandas>=0.23->pandas_datareader) (1.22.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/
dist-packages (from requests>=2.19.0->pandas_datareader) (1.26.16)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dis
t-packages (from requests>=2.19.0->pandas_datareader) (2023.5.7)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python
3.10/dist-packages (from requests>=2.19.0->pandas_datareader) (2.0.12)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-pack
ages (from requests>=2.19.0->pandas_datareader) (3.4)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages
(from python-dateutil>=2.8.1->pandas>=0.23->pandas_datareader) (1.16.0)

In [110… import pandas_datareader as pdr

In [111… yields = pdr.get_data_fred('GS10')

In [112… yields.head()

file:///C:/Users/adrie/Downloads/Python_Exercises.html 13/14
18/07/2023 18:21 Python_Exercises

Out[112]: GS10

DATE

2018-08-01 2.89

2018-09-01 3.00

2018-10-01 3.15

2018-11-01 3.12

2018-12-01 2.83

In [113… yields.plot()

<Axes: xlabel='DATE'>
Out[113]:

In [ ]:

file:///C:/Users/adrie/Downloads/Python_Exercises.html 14/14

You might also like