π Quick Study Guide: Pandas Essentials
- π‘ What is Pandas? It's a powerful Python library built on NumPy, designed for data manipulation and analysis. Think of it as Excel on steroids for programmatic use!
- π Key Data Structures:
- π’ Series: A one-dimensional labeled array capable of holding any data type (integers, strings, floats, Python objects, etc.). It's like a single column in a spreadsheet.
- π DataFrame: A two-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table. It's the most commonly used Pandas object.
- π οΈ Creating Data Structures:
- π Series: `pd.Series([10, 20, 30], index=['a', 'b', 'c'])`
- ποΈ DataFrame: `pd.DataFrame({'Col1': [1, 2], 'Col2': [3, 4]})`
- π Common Operations:
- π Reading Data: Easily load data from various sources, e.g., `pd.read_csv('filename.csv')`, `pd.read_excel('filename.xlsx')`.
- π― Selection: Access columns using `df['ColumnName']` or `df.ColumnName`. For row/column label-based selection, use `df.loc[row_label, col_label]`. For integer-location based selection, use `df.iloc[row_index, col_index]`.
- β
Filtering Data: Select rows based on conditions, e.g., `df[df['Score'] > 80]`.
- π« Handling Missing Data: Identify missing values with `df.isnull().sum()`. Drop rows/columns with `df.dropna()`. Fill missing values with `df.fillna(value)`.
- β Aggregation: Group data and apply functions, e.g., `df.groupby('Category')['Value'].mean()`.
- βοΈ Sorting: Arrange data by values in columns, e.g., `df.sort_values(by='ColumnName', ascending=True)`.
- π Dimensions: `df.shape` returns a tuple (rows, columns).
π§ Practice Quiz: Pandas Data Analysis
Test your knowledge with these multiple-choice questions! Choose the best answer for each.
- What is the primary data structure in Pandas for a 1-dimensional labeled array?
A) DataFrame
B) Series
C) Array
D) List
- How would you read a CSV file named 'data.csv' into a Pandas DataFrame?
A) `pandas.open_csv('data.csv')`
B) `pd.load_csv('data.csv')`
C) `pd.read_csv('data.csv')`
D) `pd.import_csv('data.csv')`
- To select a single column named 'Age' from a DataFrame `df`, which of the following is the most common method?
A) `df.Age`
B) `df['Age']`
C) Both A and B
D) `df(Age)`
- Which method is used to check for missing values in a Pandas DataFrame?
A) `df.has_null()`
B) `df.is_na()`
C) `df.isnull()`
D) `df.missing()`
- If you want to filter a DataFrame `df` to only include rows where the 'Score' column is greater than 80, which code snippet would you use?
A) `df[df['Score'] > 80]`
B) `df.filter(Score > 80)`
C) `df.loc[Score > 80]`
D) `df.query('Score' > 80)`
- What is the correct way to sort a DataFrame `df` by the 'Name' column in ascending order?
A) `df.order_by('Name')`
B) `df.sort_index(by='Name')`
C) `df.sort_values(by='Name', ascending=True)`
D) `df.arrange(by='Name')`
- In a Pandas DataFrame, what does `df.shape` return?
A) The data type of each column.
B) A tuple representing the dimensions (rows, columns).
C) The memory usage of the DataFrame.
D) The number of unique values in each column.
Click to see Answers
1. B) Series
2. C) `pd.read_csv('data.csv')`
3. C) Both A and B
4. C) `df.isnull()`
5. A) `df[df['Score'] > 80]`
6. C) `df.sort_values(by='Name', ascending=True)`
7. B) A tuple representing the dimensions (rows, columns).