|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "4f27cd01", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Manipulating data" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "64b13fce", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "Common data structures in Python include:\n", |
| 17 | + "- list\n", |
| 18 | + "- object\n", |
| 19 | + "- dataframe\n", |
| 20 | + "- series\n", |
| 21 | + "\n", |
| 22 | + "Means of manipulating data include:\n", |
| 23 | + "- indexing\n", |
| 24 | + "- slicing\n", |
| 25 | + "- filtering\n", |
| 26 | + "- sorting\n", |
| 27 | + "# Indexing\n", |
| 28 | + "# Indexing is used to access individual elements in a data structure.\n", |
| 29 | + "# For example, in a list, you can access the first element using an index of 0.\n", |
| 30 | + "# In a dataframe, you can access a specific column or row using its label or index.\n", |
| 31 | + "# Example of indexing in a list\n", |
| 32 | + "my_list = [10, 20, 30, 40, 50]\n", |
| 33 | + "print(my_list[0]) # Output: 10\n", |
| 34 | + "# Example of indexing in a dataframe\n", |
| 35 | + "import pandas as pd\n", |
| 36 | + "data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}\n", |
| 37 | + "df = pd.DataFrame(data)\n", |
| 38 | + "print(df['Name'][0]) # Output: Alice\n", |
| 39 | + "# Slicing\n", |
| 40 | + "# Slicing is used to access a range of elements in a data structure.\n", |
| 41 | + "# For example, in a list, you can access a sublist using a range of indices.\n", |
| 42 | + "# In a dataframe, you can access a subset of rows or columns using slicing.\n", |
| 43 | + "# Example of slicing in a list\n", |
| 44 | + "my_list = [10, 20, 30, 40, 50]\n", |
| 45 | + "print(my_list[1:4]) # Output: [20, 30, 40]\n", |
| 46 | + "# Example of slicing in a dataframe\n", |
| 47 | + "import pandas as pd\n", |
| 48 | + "data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}\n", |
| 49 | + "df = pd.DataFrame(data)\n", |
| 50 | + "print(df[1:3]) # Output: DataFrame with rows 1 and 2\n", |
| 51 | + "# Filtering\n", |
| 52 | + "# Filtering is used to access elements that meet certain conditions.\n", |
| 53 | + "# For example, in a list, you can filter elements based on a condition.\n", |
| 54 | + "# In a dataframe, you can filter rows based on column values.\n", |
| 55 | + "# Example of filtering in a list\n", |
| 56 | + "my_list = [10, 20, 30, 40, 50]\n", |
| 57 | + "filtered_list = [x for x in my_list if x > 30]\n", |
| 58 | + "print(filtered_list) # Output: [40, 50]\n", |
| 59 | + "# Example of filtering in a dataframe\n", |
| 60 | + "import pandas as pd\n", |
| 61 | + "data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}\n", |
| 62 | + "df = pd.DataFrame(data)\n", |
| 63 | + "filtered_df = df[df['Age'] > 30]\n", |
| 64 | + "print(filtered_df) # Output: DataFrame with rows where Age > 30\n", |
| 65 | + "# Sorting\n", |
| 66 | + "# Sorting is used to arrange elements in a specific order.\n", |
| 67 | + "# For example, in a list, you can sort the elements in ascending or descending order.\n", |
| 68 | + "# In a dataframe, you can sort rows based on column values.\n", |
| 69 | + "# Example of sorting in a list\n", |
| 70 | + "my_list = [50, 20, 30, 10, 40]\n", |
| 71 | + "my_list.sort()\n", |
| 72 | + "print(my_list) # Output: [10, 20, 30, 40, 50]\n", |
| 73 | + "# Example of sorting in a dataframe\n", |
| 74 | + "import pandas as pd\n", |
| 75 | + "data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}\n", |
| 76 | + "df = pd.DataFrame(data)\n", |
| 77 | + "df_sorted = df.sort_values(by='Age')\n", |
| 78 | + "print(df_sorted) # Output: DataFrame sorted by Age\n", |
| 79 | + "# Example of sorting in a dataframe with multiple columns\n", |
| 80 | + "df_sorted_multi = df.sort_values(by=['Age', 'Name'])\n", |
| 81 | + "print(df_sorted_multi) # Output: DataFrame sorted by Age, then by Name\n", |
| 82 | + "\n", |
| 83 | + "\n", |
| 84 | + "# Calculating values\n", |
| 85 | + "- Via loops\n", |
| 86 | + "- Via functions\n", |
| 87 | + "# Calculating values\n", |
| 88 | + "# Calculating values can be done using loops or functions.\n", |
| 89 | + "# Example of calculating values using a loop\n", |
| 90 | + "my_list = [1, 2, 3, 4, 5]\n", |
| 91 | + "sum_value = 0\n", |
| 92 | + "for num in my_list:\n", |
| 93 | + " sum_value += num\n", |
| 94 | + "print(sum_value) # Output: 15\n", |
| 95 | + "# Example of calculating values using a function\n", |
| 96 | + "def calculate_sum(numbers):\n", |
| 97 | + " return sum(numbers)\n", |
| 98 | + "my_list = [1, 2, 3, 4, 5]\n", |
| 99 | + "result = calculate_sum(my_list)\n", |
| 100 | + "print(result) # Output: 15\n", |
| 101 | + "# Example of calculating values using a lambda function\n", |
| 102 | + "my_list = [1, 2, 3, 4, 5]\n", |
| 103 | + "calculate_sum_lambda = lambda numbers: sum(numbers)\n", |
| 104 | + "result_lambda = calculate_sum_lambda(my_list)\n", |
| 105 | + "print(result_lambda) # Output: 15\n", |
| 106 | + "# Example of calculating values using a built-in function\n", |
| 107 | + "my_list = [1, 2, 3, 4, 5]\n", |
| 108 | + "result_builtin = sum(my_list)\n", |
| 109 | + "print(result_builtin) # Output: 15" |
| 110 | + ] |
| 111 | + } |
| 112 | + ], |
| 113 | + "metadata": { |
| 114 | + "language_info": { |
| 115 | + "name": "python" |
| 116 | + } |
| 117 | + }, |
| 118 | + "nbformat": 4, |
| 119 | + "nbformat_minor": 5 |
| 120 | +} |
0 commit comments