TensorFlowClass / pages /2_PandasExamples.py
eaglelandsonce's picture
Rename pages/2_PandasIntro.py to pages/2_PandasExamples.py
40eeee3 verified
raw
history blame
7.62 kB
import streamlit as st
import pandas as pd
# Example functions with explanations
def example1():
explanation = "Creating a DataFrame from a dictionary."
code = "df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]})\ndf"
df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]})
return explanation, code, df
def example2():
explanation = "Creating a simple DataFrame."
code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf"
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
return explanation, code, df
def example3():
explanation = "Reading a CSV file (using a sample DataFrame)."
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"
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.to_csv('sample.csv', index=False)
df = pd.read_csv('sample.csv')
return explanation, code, df
def example4():
explanation = "Displaying the first few rows of a DataFrame."
code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf.head()"
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
return explanation, code, df.head()
def example5():
explanation = "Selecting a column from a DataFrame."
code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf['A']"
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
return explanation, code, df['A']
def example6():
explanation = "Selecting multiple columns from a DataFrame."
code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})\ndf[['A', 'C']]"
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
return explanation, code, df[['A', 'C']]
def example7():
explanation = "Filtering rows based on a condition."
code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf[df['A'] > 1]"
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
return explanation, code, df[df['A'] > 1]
def example8():
explanation = "Adding a new column to a DataFrame."
code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf['C'] = df['A'] + df['B']\ndf"
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df['C'] = df['A'] + df['B']
return explanation, code, df
def example9():
explanation = "Dropping a column from a DataFrame."
code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})\ndf.drop('B', axis=1)"
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
return explanation, code, df.drop('B', axis=1)
def example10():
explanation = "Renaming columns in a DataFrame."
code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf.rename(columns={'A': 'a', 'B': 'b'})"
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
return explanation, code, df.rename(columns={'A': 'a', 'B': 'b'})
def example11():
explanation = "Handling missing data by filling NaNs."
code = "df = pd.DataFrame({'A': [1, 2, None], 'B': [4, None, 6]})\ndf.fillna(0)"
df = pd.DataFrame({'A': [1, 2, None], 'B': [4, None, 6]})
return explanation, code, df.fillna(0)
def example12():
explanation = "Handling missing data by dropping NaNs."
code = "df = pd.DataFrame({'A': [1, None, 3], 'B': [None, 5, 6]})\ndf.dropna()"
df = pd.DataFrame({'A': [1, None, 3], 'B': [None, 5, 6]})
return explanation, code, df.dropna()
def example13():
explanation = "Group by and aggregate a DataFrame."
code = "df = pd.DataFrame({'A': ['foo', 'bar', 'foo'], 'B': [1, 2, 3]})\ndf.groupby('A').sum()"
df = pd.DataFrame({'A': ['foo', 'bar', 'foo'], 'B': [1, 2, 3]})
return explanation, code, df.groupby('A').sum()
def example14():
explanation = "Merging two DataFrames."
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')"
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [1, 2, 3], 'C': [7, 8, 9]})
return explanation, code, df1.merge(df2, on='A')
def example15():
explanation = "Concatenating two DataFrames."
code = "df1 = pd.DataFrame({'A': [1, 2, 3]})\ndf2 = pd.DataFrame({'A': [4, 5, 6]})\npd.concat([df1, df2])"
df1 = pd.DataFrame({'A': [1, 2, 3]})
df2 = pd.DataFrame({'A': [4, 5, 6]})
return explanation, code, pd.concat([df1, df2])
def example16():
explanation = "Pivoting a DataFrame (using unique index)."
code = "df = pd.DataFrame({'A': ['foo', 'bar', 'baz'], 'B': ['one', 'two', 'one'], 'C': [1, 2, 3]})\ndf.pivot(index='A', columns='B', values='C')"
df = pd.DataFrame({'A': ['foo', 'bar', 'baz'], 'B': ['one', 'two', 'one'], 'C': [1, 2, 3]})
return explanation, code, df.pivot(index='A', columns='B', values='C')
def example17():
explanation = "Melting a DataFrame."
code = "df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})\npd.melt(df, id_vars=['A'], value_vars=['B', 'C'])"
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})
return explanation, code, pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
def example18():
explanation = "Applying a function to a DataFrame."
code = "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})\ndf.apply(lambda x: x + 1)"
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
return explanation, code, df.apply(lambda x: x + 1)
def example19():
explanation = "Joining two DataFrames."
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)"
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'C': [7, 8, 9]}, index=[0, 1, 2])
return explanation, code, df1.join(df2)
def example20():
explanation = "Sorting a DataFrame by a column."
code = "df = pd.DataFrame({'A': [3, 2, 1], 'B': [6, 5, 4]})\ndf.sort_values(by='A')"
df = pd.DataFrame({'A': [3, 2, 1], 'B': [6, 5, 4]})
return explanation, code, df.sort_values(by='A')
examples = [
("Example 1: Creating a DataFrame from a dictionary", example1),
("Example 2: Create a simple DataFrame", example2),
("Example 3: Read a CSV file", example3),
("Example 4: Display the first few rows of a DataFrame", example4),
("Example 5: Select a column from a DataFrame", example5),
("Example 6: Select multiple columns from a DataFrame", example6),
("Example 7: Filter rows based on a condition", example7),
("Example 8: Add a new column to a DataFrame", example8),
("Example 9: Drop a column from a DataFrame", example9),
("Example 10: Rename columns in a DataFrame", example10),
("Example 11: Handle missing data by filling NaNs", example11),
("Example 12: Handle missing data by dropping NaNs", example12),
("Example 13: Group by and aggregate a DataFrame", example13),
("Example 14: Merge two DataFrames", example14),
("Example 15: Concatenate two DataFrames", example15),
("Example 16: Pivot a DataFrame (using unique index)", example16),
("Example 17: Melt a DataFrame", example17),
("Example 18: Apply a function to a DataFrame", example18),
("Example 19: Join two DataFrames", example19),
("Example 20: Sort a DataFrame by a column", example20),
]
st.title("Pandas Course with Streamlit")
for title, func in examples:
st.header(title)
explanation, code, result = func()
st.write(explanation)
st.code(code)
if st.button(f"Run {title.split(':')[0]}"):
if result is not None:
st.write("Output:", result)