{"id":723,"date":"2023-12-16T18:22:14","date_gmt":"2023-12-16T18:22:14","guid":{"rendered":"https:\/\/emorah.com\/story\/?p=723"},"modified":"2023-12-16T18:22:16","modified_gmt":"2023-12-16T18:22:16","slug":"fresco-play-python-pandas-hands-on-solution-t-factor","status":"publish","type":"post","link":"https:\/\/emorah.com\/story\/fresco-play\/fresco-play-python-pandas-hands-on-solution-t-factor\/","title":{"rendered":"Fresco Play Python Pandas Hands- on Solution || T Factor"},"content":{"rendered":"\n<p><strong>Fresco Play Python Pandas Hands- on Solution || T Factor<\/strong><\/p>\n\n\n\n<h1>Fresco Play Python Pandas Hands-on Solution &#8211; T Factor (Course ID:- 55937)<\/h1>\n\n\n\n<p>In Python Pandas(Course Id:- 55937), There are 8 Hands-On Questions Available. The Solutions are\u00a0<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ol><li>Data Structures in Pandas Solution.<\/li><\/ol>\n\n\n\n<p>Code: -#Write your code here<br>import pandas as pd<br>import numpy as np<br>heights_A = pd.Series([176.2, 158.4, 167.6, 156.2, 161.4])<br>heights_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<br>print(heights_A.shape)<\/p>\n\n\n\n<h1>TASK 2<\/h1>\n\n\n\n<p>weights_A = pd.Series([85.1, 90.2, 76.8, 80.4, 78.9])<br>weights_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<br>print(weights_A.dtype)<\/p>\n\n\n\n<h1>TASK 3<\/h1>\n\n\n\n<p>df_A = pd.DataFrame()<br>df_A[&#8216;Student_height&#8217;] = heights_A<br>df_A[&#8216;Student_weight&#8217;] = weights_A<br>print(df_A.shape)<\/p>\n\n\n\n<h1>TASK 4<\/h1>\n\n\n\n<p>my_mean = 170.0<br>my_std = 25.0<br>np.random.seed(100)<br>heights_B = pd.Series(np.random.normal(loc = my_mean, scale = my_std, size = 5))<br>heights_B.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<\/p>\n\n\n\n<p>my_mean1 = 75.0<br>my_std1 = 12.0<br>weights_B = pd.Series(np.random.normal(loc = my_mean1, scale = my_std1, size = 5))<br>weights_B.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<br>print(heights_B.mean())<\/p>\n\n\n\n<h1>TASK 5<\/h1>\n\n\n\n<p>df_B = pd.DataFrame()<br>df_B[&#8216;Student_height&#8217;] = heights_B<br>df_B[&#8216;Student_weight&#8217;] = weights_B<br>print(df_B.columns)<\/p>\n\n\n\n<h1>TASK 6<\/h1>\n\n\n\n<p>data = {<br>&#8216;ClassA&#8217;: df_A,<br>&#8216;ClassB&#8217;: df_B<br>}<br>p = pd.Panel.from_dict(data)<br>print(p.shape)<\/p>\n\n\n\n<ol start=\"2\"><li>Data Cleaning Solutions &#8211; Python Pandas<\/li><\/ol>\n\n\n\n<p>Code: -#Write your code here<br>import pandas as pd<br>import numpy as np<br>height_A = pd.Series([176.2, 158.4, 167.6, 156.2, 161.4])<br>height_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<br>weight_A = pd.Series([85.1, 90.2, 76.8, 80.4, 78.9])<br>weight_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<br>df_A = pd.DataFrame()<br>df_A[&#8216;Student_height&#8217;] = height_A<br>df_A[&#8216;Student_weight&#8217;] = weight_A<\/p>\n\n\n\n<p>df_A.loc[&#8216;s3&#8217;] = np.nan<br>df_A.loc[&#8216;s5&#8217;][1] = np.nan<\/p>\n\n\n\n<p>df_A2 = df_A.dropna(how = &#8216;any&#8217;)<br>print(df_A2)<\/p>\n\n\n\n<ol start=\"3\"><li>Data Merging Hands &#8211; On(2) Solution: Python Pandas<\/li><\/ol>\n\n\n\n<p>Code: -#Write your code here<br>import pandas as pd<br>import numpy as np<\/p>\n\n\n\n<p>height_A = pd.Series([176.2, 158.4, 167.6, 156.2, 161.4])<br>height_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<\/p>\n\n\n\n<p>weights_A = pd.Series([85.1, 90.2, 76.8, 80.4, 78.9])<br>weights_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<\/p>\n\n\n\n<p>df_A = pd.DataFrame()<br>df_A[&#8216;Student_height&#8217;] = height_A<br>df_A[&#8216;Student_weight&#8217;] = weights_A<\/p>\n\n\n\n<p>df_A[&#8216;Gender&#8217;] = [&#8216;M&#8217;, &#8216;F&#8217;, &#8216;M&#8217;, &#8216;M&#8217;, &#8216;F&#8217;]<\/p>\n\n\n\n<p>s = pd.Series([165.4, 82.7, &#8216;F&#8217;], index = [&#8216;Student_height&#8217;, &#8216;Student_weight&#8217;, &#8216;Gender&#8217;], name = &#8216;s6&#8217;)<\/p>\n\n\n\n<p>df_AA = df_A.append(s)<br>print(df_AA)<\/p>\n\n\n\n<h1>TASK &#8211; 2<\/h1>\n\n\n\n<p>my_mean = 170.0<br>my_std = 25.0<br>np.random.seed(100)<br>heights_B = pd.Series(np.random.normal(loc = my_mean, scale = my_std, size = 5))<br>heights_B.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<\/p>\n\n\n\n<p>my_mean1 = 75.0<br>my_std1 = 12.0<br>np.random.seed(100)<br>weights_B = pd.Series(np.random.normal(loc = my_mean1, scale = my_std1, size = 5))<br>weights_B.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<\/p>\n\n\n\n<p>df_B = pd.DataFrame()<br>df_B[&#8216;Student_height&#8217;] = heights_B<br>df_B[&#8216;Student_weight&#8217;] = weights_B<\/p>\n\n\n\n<p>df_B.index = [&#8216;s7&#8217;, &#8216;s8&#8217;, &#8216;s9&#8217;, &#8216;s10&#8217;, &#8216;s11&#8217;]<br>df_B[&#8216;Gender&#8217;] = [&#8216;F&#8217;, &#8216;M&#8217;, &#8216;F&#8217;, &#8216;F&#8217;, &#8216;M&#8217;]<\/p>\n\n\n\n<p>df = pd.concat([df_AA, df_B])<br>print(df)<\/p>\n\n\n\n<ol start=\"4\"><li>Data Merging Hands &#8211; On(1) Solutions: -Python Pandas<\/li><\/ol>\n\n\n\n<p>Code: -#Write your code here<br>import pandas as pd<br>import numpy as np<br>nameid = pd.Series(range(101, 111))<br>name = pd.Series([&#8216;person&#8217; + str(i) for i in range(1, 11)])<br>master = pd.DataFrame()<br>master[&#8216;nameid&#8217;] = nameid<br>master[&#8216;name&#8217;] = name<br>transaction = pd.DataFrame({<br>&#8216;nameid&#8217;: [108, 108, 108, 103],<br>&#8216;product&#8217;: [&#8216;iPhone&#8217;, &#8216;Nokia&#8217;, &#8216;Micromax&#8217;, &#8216;Vivo&#8217;]<br>})<br>mdf = pd.merge(master, transaction, on = &#8216;nameid&#8217;)<br>print(mdf)<\/p>\n\n\n\n<ol start=\"5\"><li>Indexing Dataframe Hands &#8211; On Solutions &#8211; Python Pandas<\/li><\/ol>\n\n\n\n<p>Code: &#8211;<br>import pandas as pd<br>import numpy as np<\/p>\n\n\n\n<h1>TASK &#8211; 1<\/h1>\n\n\n\n<p>DatetimeIndex = pd.date_range(start = &#8217;09\/01\/2017&#8242;, end = &#8217;09\/15\/2017&#8242;)<br>print(DatetimeIndex[2])<\/p>\n\n\n\n<h1>TASK &#8211; 2<\/h1>\n\n\n\n<p>datelist = [&#8217;14-Sep-2017&#8242;, &#8217;09-Sep-2017&#8242;]<br>date_to_be_searched = pd.to_datetime(datelist)<br>print(date_to_be_searched)<\/p>\n\n\n\n<h1>TASK &#8211; 3<\/h1>\n\n\n\n<p>print(date_to_be_searched.isin(datelist))<\/p>\n\n\n\n<h1>TASK &#8211; 4<\/h1>\n\n\n\n<p>arraylist = [<br>[&#8216;classA&#8217;] * 5 + [&#8216;classB&#8217;] * 5, [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;] * 2<br>]<br>mi_index = pd.MultiIndex.from_product(arraylist, names = [&#8216;First Level&#8217;, &#8216;Second Level&#8217;])<br>print(mi_index.levels)<\/p>\n\n\n\n<ol start=\"6\"><li>Data Aggression: -Python Pandas<\/li><\/ol>\n\n\n\n<p>Code: -#Write your code here<br>import pandas as pd<br>import numpy as np<br>heights_A = pd.Series([176.2, 158.4, 167.6, 156.2, 161.4])<br>heights_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<br>weights_A = pd.Series([85.1, 90.2, 76.8, 80.4, 78.9])<br>weights_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<\/p>\n\n\n\n<p>df_A = pd.DataFrame()<\/p>\n\n\n\n<p>df_A[&#8216;Student_height&#8217;] = heights_A<br>df_A[&#8216;Student_weight&#8217;] = weights_A<\/p>\n\n\n\n<p>df_A_filter1 = df_A[(df_A.Student_weight &lt; 80.0) &amp; (df_A.Student_height &gt; 160.0)]<br>print(df_A_filter1)<\/p>\n\n\n\n<h1>TASK &#8211; 2<\/h1>\n\n\n\n<p>df_A_filter2 = df_A[df_A.index.isin([&#8216;s5&#8217;])]<br>print(df_A_filter2)<\/p>\n\n\n\n<h1>TASK &#8211; 3<\/h1>\n\n\n\n<p>df_A[&#8216;Gender&#8217;] = [&#8216;M&#8217;, &#8216;F&#8217;, &#8216;M&#8217;, &#8216;M&#8217;, &#8216;F&#8217;]<br>df_groups = df_A.groupby(&#8216;Gender&#8217;)<br>print(df_groups.mean())<\/p>\n\n\n\n<ol start=\"7\"><li>Accessing Pandas Data Structures &#8211; Python Pandas<\/li><\/ol>\n\n\n\n<p>Code: &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Write your code here<\/code><\/pre>\n\n\n\n<p>import pandas as pd<br>import numpy as np<\/p>\n\n\n\n<p>heights_A = pd.Series([176.2, 158.4, 167.6, 156.2, 161.4])<br>heights_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<br>print(heights_A[1])<\/p>\n\n\n\n<h1>TASK 2<\/h1>\n\n\n\n<p>print(heights_A[1: 4])<\/p>\n\n\n\n<h1>TASK 3<\/h1>\n\n\n\n<p>weights_A = pd.Series([85.1, 90.2, 76.8, 80.4, 78.9])<br>weights_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<\/p>\n\n\n\n<p>df_A = pd.DataFrame()<br>df_A[&#8216;Student_height&#8217;] = heights_A<br>df_A[&#8216;Student_weight&#8217;] = weights_A<\/p>\n\n\n\n<p>height = df_A[&#8216;Student_height&#8217;]<br>print(type(height))<\/p>\n\n\n\n<h1>TASK 4<\/h1>\n\n\n\n<p>df_s1s2 = df_A[df_A.index.isin([&#8216;s1&#8217;, &#8216;s2&#8217;])]<br>print(df_s1s2)<\/p>\n\n\n\n<h1>TASK 5<\/h1>\n\n\n\n<p>df_s2s5s1 = df_A[df_A.index.isin([&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s5&#8217;])]<br>df_s2s5s1 = df_s2s5s1.reindex([&#8216;s2&#8217;, &#8216;s5&#8217;, &#8216;s1&#8217;])<br>print(df_s2s5s1)<\/p>\n\n\n\n<h1>TASK 6<\/h1>\n\n\n\n<p>df_s1s4 = df_A[df_A.index.isin([&#8216;s1&#8217;, &#8216;s4&#8217;])]<br>print(df_s1s4)<\/p>\n\n\n\n<ol start=\"8\"><li>Working With CSV Files<\/li><\/ol>\n\n\n\n<p>Code: &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Write your code here<\/code><\/pre>\n\n\n\n<p>import pandas as pd<br>import numpy as np<br>heights_A = pd.Series([176.2, 158.4, 167.6, 156.2, 161.4])<br>heights_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<br>weights_A = pd.Series([85.1, 90.2, 76.8, 80.4, 78.9])<br>weights_A.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<br>df_A = pd.DataFrame()<br>df_A[&#8216;Student_height&#8217;] = heights_A<br>df_A[&#8216;Student_weight&#8217;] = weights_A<br>df_A.to_csv(&#8216;classA.csv&#8217;)<\/p>\n\n\n\n<h1>TASK 2<\/h1>\n\n\n\n<p>df_A2 = pd.read_csv(&#8216;classA.csv&#8217;)<br>print(df_A2)<\/p>\n\n\n\n<h1>TASK 3<\/h1>\n\n\n\n<p>df_A3 = pd.read_csv(&#8216;classA.csv&#8217;, index_col = 0)<br>print(df_A3)<\/p>\n\n\n\n<h1>TASK 4<\/h1>\n\n\n\n<p>my_mean = 170.0<br>my_std = 25.0<br>np.random.seed(100)<br>heights_B = pd.Series(np.random.normal(loc = my_mean, scale = my_std, size = 5))<br>heights_B.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<br>my_mean1 = 75.0<br>my_std1 = 12.0<br>np.random.seed(100)<br>weights_B = pd.Series(np.random.normal(loc = my_mean1, scale = my_std1, size = 5))<br>weights_B.index = [&#8216;s1&#8217;, &#8216;s2&#8217;, &#8216;s3&#8217;, &#8216;s4&#8217;, &#8216;s5&#8217;]<br>df_B = pd.DataFrame()<br>df_B[&#8216;Student_height&#8217;] = heights_B<br>df_B[&#8216;Student_weight&#8217;] = weights_B<\/p>\n\n\n\n<p>df_B.to_csv(&#8216;classB.csv&#8217;, index = False)<br>print(&#8216;classB.csv&#8217;)<\/p>\n\n\n\n<h1>TASK 5<\/h1>\n\n\n\n<p>df_B2 = pd.read_csv(&#8216;classB.csv&#8217;)<br>print(df_B2)<\/p>\n\n\n\n<h1>TASK 6<\/h1>\n\n\n\n<p>df_B3 = pd.read_csv(&#8216;classB.csv&#8217;, header = None)<br>print(df_B3)<\/p>\n\n\n\n<h1>TASK 7<\/h1>\n\n\n\n<p>df_B4 = pd.read_csv(&#8216;classB.csv&#8217;, header = None, skiprows = 2)<br>print(df_B4)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fresco Play Python Pandas Hands- on Solution || T Factor Fresco Play Python Pandas Hands-on Solution &#8211; T Factor (Course ID:- 55937) In Python Pandas(Course Id:- 55937), There are 8 Hands-On Questions Available. The Solutions are\u00a0 Data Structures in Pandas Solution. Code: -#Write your code hereimport pandas as pdimport numpy [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":663,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":""},"categories":[156],"tags":[],"_links":{"self":[{"href":"https:\/\/emorah.com\/story\/wp-json\/wp\/v2\/posts\/723"}],"collection":[{"href":"https:\/\/emorah.com\/story\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emorah.com\/story\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emorah.com\/story\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/emorah.com\/story\/wp-json\/wp\/v2\/comments?post=723"}],"version-history":[{"count":1,"href":"https:\/\/emorah.com\/story\/wp-json\/wp\/v2\/posts\/723\/revisions"}],"predecessor-version":[{"id":724,"href":"https:\/\/emorah.com\/story\/wp-json\/wp\/v2\/posts\/723\/revisions\/724"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/emorah.com\/story\/wp-json\/wp\/v2\/media\/663"}],"wp:attachment":[{"href":"https:\/\/emorah.com\/story\/wp-json\/wp\/v2\/media?parent=723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emorah.com\/story\/wp-json\/wp\/v2\/categories?post=723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emorah.com\/story\/wp-json\/wp\/v2\/tags?post=723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}