Lesson 28 of 30
In Progress

# Introduction to NumPy and Pandas

##### Yasin Cakal

NumPy and Pandas are two of the most popular Python libraries for data science. They are widely used for data manipulation and analysis, and are essential tools for anyone working with data in Python.

## NumPy

NumPy, or Numeric Python, is a library for scientific computing with Python. It provides functions for working with arrays, matrices, and numerical operations such as linear algebra and random number generation. NumPy is particularly useful for working with large, multi-dimensional arrays, and is an important tool for scientific computing and data analysis.

## Pandas

Pandas, or Python Data Analysis Library, is a library for data manipulation and analysis. It provides functions for reading and writing data from various formats (such as CSV, Excel, and SQL), as well as functions for cleaning, filtering, and aggregating data. Pandas is particularly useful for working with tabular data, such as spreadsheets or databases, and is an essential tool for data preparation and analysis.

## Using NumPy

To use NumPy and Pandas in your Python code, you will need to import them first. This can be done using the following import statements:

import numpy as np
import pandas as pd


With these imports, you can then use NumPy and Pandas functions by prefixing them with np and pd, respectively. For example, to create a NumPy array, you can use the np.array function:

a = np.array([1, 2, 3])
print(a)  # [1 2 3]


## Using Pandas

To create a Pandas DataFrame, you can use the pd.DataFrame function:

data = {'col1': [1, 2, 3], 'col2': [4, 5, 6]}
df = pd.DataFrame(data)
print(df)


This would output the following DataFrame:

   col1  col2
0     1     4
1     2     5
2     3     6


## Conclusion

NumPy and Pandas are powerful tools for data manipulation and analysis, and are essential for anyone working with data in Python. Whether you are a data scientist, data analyst, or simply someone who needs to work with data, these libraries will be an invaluable resource.

## Exercises

Here are some exercises with solutions to help you practice what you just learned:

#### Create a NumPy array of 10 random integers between 1 and 100, and print the minimum and maximum values.

import numpy as np

# Create the array
arr = np.random.randint(1, 101, 10)

# Print the minimum and maximum values
print(f"Minimum value: {arr.min()}")
print(f"Maximum value: {arr.max()}")


Solution:

Minimum value: 7

Maximum value: 95

#### Create a Pandas DataFrame from a dictionary of lists, and select the first three rows.

import pandas as pd

# Create the DataFrame
data = {'col1': [1, 2, 3, 4, 5], 'col2': [6, 7, 8, 9, 10]}
df = pd.DataFrame(data)

# Select the first three rows
first_three = df[:3]
print(first_three)


Solution:

   col1  col2
0     1     6
1     2     7
2     3     8


#### Load a CSV file into a Pandas DataFrame, and select the rows where the value in the ‘col1’ column is greater than 5.

import pandas as pd

# Load the CSV file into a DataFrame

# Select rows where 'col1' is greater than 5
selected = df[df['col1'] > 5]
print(selected)


#### Use NumPy to create an array of 10 random floating point numbers, and round them to the nearest whole number.

import numpy as np

# Create the array of random floating point numbers
arr = np.random.rand(10)

# Round the numbers to the nearest whole number
rounded = np.round(arr)
print(rounded)


#### Use Pandas to group a DataFrame by the values in a column, and calculate the mean of each group.

import pandas as pd

# Create the DataFrame
data = {'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'col2': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
df = pd.DataFrame(data)

# Group the DataFrame by the values in 'col1' and calculate the mean of each group
grouped = df.groupby('col1').mean()
print(grouped)


Solution:

col1   col2
1       1.0
2       2.0
3       3.0
4       4.0
5       5.0
6       6.0
7       7.0
8       8.0
9       9.0
10     10.0