Additional Tutorials
Python Basics
Manipulating data
Common data structures in Python include:
- list
- object
- dataframe
- series
Means of manipulating data include:
- indexing
- slicing
- filtering
- sorting
# Indexing
# Indexing is used to access individual elements in a data structure.
# For example, in a list, you can access the first element using an index of 0.
# In a dataframe, you can access a specific column or row using its label or index.
# Example of indexing in a list
my_list = [10, 20, 30, 40, 50]
print(my_list[0]) # Output: 10
# Example of indexing in a dataframe
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
print(df['Name'][0]) # Output: Alice
# Slicing
# Slicing is used to access a range of elements in a data structure.
# For example, in a list, you can access a sublist using a range of indices.
# In a dataframe, you can access a subset of rows or columns using slicing.
# Example of slicing in a list
my_list = [10, 20, 30, 40, 50]
print(my_list[1:4]) # Output: [20, 30, 40]
# Example of slicing in a dataframe
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
print(df[1:3]) # Output: DataFrame with rows 1 and 2
# Filtering
# Filtering is used to access elements that meet certain conditions.
# For example, in a list, you can filter elements based on a condition.
# In a dataframe, you can filter rows based on column values.
# Example of filtering in a list
my_list = [10, 20, 30, 40, 50]
filtered_list = [x for x in my_list if x > 30]
print(filtered_list) # Output: [40, 50]
# Example of filtering in a dataframe
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
filtered_df = df[df['Age'] > 30]
print(filtered_df) # Output: DataFrame with rows where Age > 30
# Sorting
# Sorting is used to arrange elements in a specific order.
# For example, in a list, you can sort the elements in ascending or descending order.
# In a dataframe, you can sort rows based on column values.
# Example of sorting in a list
my_list = [50, 20, 30, 10, 40]
my_list.sort()
print(my_list) # Output: [10, 20, 30, 40, 50]
# Example of sorting in a dataframe
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
df_sorted = df.sort_values(by='Age')
print(df_sorted) # Output: DataFrame sorted by Age
# Example of sorting in a dataframe with multiple columns
df_sorted_multi = df.sort_values(by=['Age', 'Name'])
print(df_sorted_multi) # Output: DataFrame sorted by Age, then by Name
# Calculating values
- Via loops
- Via functions
# Calculating values
# Calculating values can be done using loops or functions.
# Example of calculating values using a loop
my_list = [1, 2, 3, 4, 5]
sum_value = 0
for num in my_list:
sum_value += num
print(sum_value) # Output: 15
# Example of calculating values using a function
def calculate_sum(numbers):
return sum(numbers)
my_list = [1, 2, 3, 4, 5]
result = calculate_sum(my_list)
print(result) # Output: 15
# Example of calculating values using a lambda function
my_list = [1, 2, 3, 4, 5]
calculate_sum_lambda = lambda numbers: sum(numbers)
result_lambda = calculate_sum_lambda(my_list)
print(result_lambda) # Output: 15
# Example of calculating values using a built-in function
my_list = [1, 2, 3, 4, 5]
result_builtin = sum(my_list)
print(result_builtin) # Output: 15