eaglelandsonce commited on
Commit
6c42acb
·
verified ·
1 Parent(s): bb1247e

Create 2_PandasIntro.py

Browse files
Files changed (1) hide show
  1. pages/2_PandasIntro.py +163 -0
pages/2_PandasIntro.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+
4
+ # Example functions with explanations
5
+ def example1():
6
+ explanation = "Importing the Pandas library."
7
+ code = "import pandas as pd"
8
+ return explanation, code, None
9
+
10
+ def example2():
11
+ explanation = "Creating a simple DataFrame."
12
+ code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf"
13
+ df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
14
+ return explanation, code, df
15
+
16
+ def example3():
17
+ explanation = "Reading a CSV file (using a sample DataFrame)."
18
+ code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf.to_csv('sample.csv', index=False)\ndf = pd.read_csv('sample.csv')\ndf"
19
+ df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
20
+ df.to_csv('sample.csv', index=False)
21
+ df = pd.read_csv('sample.csv')
22
+ return explanation, code, df
23
+
24
+ def example4():
25
+ explanation = "Displaying the first few rows of a DataFrame."
26
+ code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf.head()"
27
+ df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
28
+ return explanation, code, df.head()
29
+
30
+ def example5():
31
+ explanation = "Selecting a column from a DataFrame."
32
+ code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf['A']"
33
+ df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
34
+ return explanation, code, df['A']
35
+
36
+ def example6():
37
+ explanation = "Selecting multiple columns from a DataFrame."
38
+ code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})\ndf[['A', 'C']]"
39
+ df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
40
+ return explanation, code, df[['A', 'C']]
41
+
42
+ def example7():
43
+ explanation = "Filtering rows based on a condition."
44
+ code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf[df['A'] > 1]"
45
+ df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
46
+ return explanation, code, df[df['A'] > 1]
47
+
48
+ def example8():
49
+ explanation = "Adding a new column to a DataFrame."
50
+ code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf['C'] = df['A'] + df['B']\ndf"
51
+ df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
52
+ df['C'] = df['A'] + df['B']
53
+ return explanation, code, df
54
+
55
+ def example9():
56
+ explanation = "Dropping a column from a DataFrame."
57
+ code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})\ndf.drop('B', axis=1)"
58
+ df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
59
+ return explanation, code, df.drop('B', axis=1)
60
+
61
+ def example10():
62
+ explanation = "Renaming columns in a DataFrame."
63
+ code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf.rename(columns={'A': 'a', 'B': 'b'})"
64
+ df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
65
+ return explanation, code, df.rename(columns={'A': 'a', 'B': 'b'})
66
+
67
+ def example11():
68
+ explanation = "Handling missing data by filling NaNs."
69
+ code = "df = pd.DataFrame({'A': [1, 2, None], 'B': [4, None, 6]})\ndf.fillna(0)"
70
+ df = pd.DataFrame({'A': [1, 2, None], 'B': [4, None, 6]})
71
+ return explanation, code, df.fillna(0)
72
+
73
+ def example12():
74
+ explanation = "Handling missing data by dropping NaNs."
75
+ code = "df = pd.DataFrame({'A': [1, None, 3], 'B': [None, 5, 6]})\ndf.dropna()"
76
+ df = pd.DataFrame({'A': [1, None, 3], 'B': [None, 5, 6]})
77
+ return explanation, code, df.dropna()
78
+
79
+ def example13():
80
+ explanation = "Group by and aggregate a DataFrame."
81
+ code = "df = pd.DataFrame({'A': ['foo', 'bar', 'foo'], 'B': [1, 2, 3]})\ndf.groupby('A').sum()"
82
+ df = pd.DataFrame({'A': ['foo', 'bar', 'foo'], 'B': [1, 2, 3]})
83
+ return explanation, code, df.groupby('A').sum()
84
+
85
+ def example14():
86
+ explanation = "Merging two DataFrames."
87
+ code = "df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf2 = pd.DataFrame({'A': [1, 2, 3], 'C': [7, 8, 9]})\ndf1.merge(df2, on='A')"
88
+ df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
89
+ df2 = pd.DataFrame({'A': [1, 2, 3], 'C': [7, 8, 9]})
90
+ return explanation, code, df1.merge(df2, on='A')
91
+
92
+ def example15():
93
+ explanation = "Concatenating two DataFrames."
94
+ code = "df1 = pd.DataFrame({'A': [1, 2, 3]})\ndf2 = pd.DataFrame({'A': [4, 5, 6]})\npd.concat([df1, df2])"
95
+ df1 = pd.DataFrame({'A': [1, 2, 3]})
96
+ df2 = pd.DataFrame({'A': [4, 5, 6]})
97
+ return explanation, code, pd.concat([df1, df2])
98
+
99
+ def example16():
100
+ explanation = "Pivoting a DataFrame."
101
+ code = "df = pd.DataFrame({'A': ['foo', 'bar', 'foo'], 'B': ['one', 'two', 'one'], 'C': [1, 2, 3]})\ndf.pivot(index='A', columns='B', values='C')"
102
+ df = pd.DataFrame({'A': ['foo', 'bar', 'foo'], 'B': ['one', 'two', 'one'], 'C': [1, 2, 3]})
103
+ return explanation, code, df.pivot(index='A', columns='B', values='C')
104
+
105
+ def example17():
106
+ explanation = "Melting a DataFrame."
107
+ code = "df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})\npd.melt(df, id_vars=['A'], value_vars=['B', 'C'])"
108
+ df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})
109
+ return explanation, code, pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
110
+
111
+ def example18():
112
+ explanation = "Applying a function to a DataFrame."
113
+ code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf.apply(lambda x:
114
+ df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf.apply(lambda x: x + 1)"
115
+ df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
116
+ return explanation, code, df.apply(lambda x: x + 1)
117
+
118
+ def example19():
119
+ explanation = "Joining two DataFrames."
120
+ code = "df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf2 = pd.DataFrame({'C': [7, 8, 9]}, index=[0, 1, 2])\ndf1.join(df2)"
121
+ df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
122
+ df2 = pd.DataFrame({'C': [7, 8, 9]}, index=[0, 1, 2])
123
+ return explanation, code, df1.join(df2)
124
+
125
+ def example20():
126
+ explanation = "Sorting a DataFrame by a column."
127
+ code = "df = pd.DataFrame({'A': [3, 2, 1], 'B': [6, 5, 4]})\ndf.sort_values(by='A')"
128
+ df = pd.DataFrame({'A': [3, 2, 1], 'B': [6, 5, 4]})
129
+ return explanation, code, df.sort_values(by='A')
130
+
131
+ examples = [
132
+ ("Example 1: Import Pandas", example1),
133
+ ("Example 2: Create a simple DataFrame", example2),
134
+ ("Example 3: Read a CSV file", example3),
135
+ ("Example 4: Display the first few rows of a DataFrame", example4),
136
+ ("Example 5: Select a column from a DataFrame", example5),
137
+ ("Example 6: Select multiple columns from a DataFrame", example6),
138
+ ("Example 7: Filter rows based on a condition", example7),
139
+ ("Example 8: Add a new column to a DataFrame", example8),
140
+ ("Example 9: Drop a column from a DataFrame", example9),
141
+ ("Example 10: Rename columns in a DataFrame", example10),
142
+ ("Example 11: Handle missing data by filling NaNs", example11),
143
+ ("Example 12: Handle missing data by dropping NaNs", example12),
144
+ ("Example 13: Group by and aggregate a DataFrame", example13),
145
+ ("Example 14: Merge two DataFrames", example14),
146
+ ("Example 15: Concatenate two DataFrames", example15),
147
+ ("Example 16: Pivot a DataFrame", example16),
148
+ ("Example 17: Melt a DataFrame", example17),
149
+ ("Example 18: Apply a function to a DataFrame", example18),
150
+ ("Example 19: Join two DataFrames", example19),
151
+ ("Example 20: Sort a DataFrame by a column", example20),
152
+ ]
153
+
154
+ st.title("Pandas Course with Streamlit")
155
+
156
+ for title, func in examples:
157
+ st.header(title)
158
+ explanation, code, result = func()
159
+ st.write(explanation)
160
+ st.code(code)
161
+ if st.button(f"Run {title.split(':')[0]}"):
162
+ if result is not None:
163
+ st.write("Output:", result)