0% found this document useful (0 votes)
4 views11 pages

Random Module Functions

The document provides an overview of various functions in Python's Random Module for generating random numbers, as well as methods for saving and loading 3D NumPy arrays using the savetxt() and loadtxt() functions. It also introduces the Pandas library, detailing its data structures (Series and DataFrame), operations for data manipulation, and examples of creating DataFrames from different data sources. Additionally, it explains how to read CSV files into a Pandas DataFrame.

Uploaded by

dharshmari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Random Module Functions

The document provides an overview of various functions in Python's Random Module for generating random numbers, as well as methods for saving and loading 3D NumPy arrays using the savetxt() and loadtxt() functions. It also introduces the Pandas library, detailing its data structures (Series and DataFrame), operations for data manipulation, and examples of creating DataFrames from different data sources. Additionally, it explains how to read CSV files into a Pandas DataFrame.

Uploaded by

dharshmari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Random Module Functions

There are different random functions in the Random Module of Python.


Look at the table below to learn more about these functions:

Function Name Description

Initialize the random number


seed() generator

Returns an object with the current


internal state of the random
getstate() number generator

Used to restore the state of the


random number generator back
setstate() to the specified state

Return an integer with a specified


getrandbits() number of bits

Returns a random number within


randrange() the range

Returns a random integer within


randint() the range

Returns a random item from a list,


choice() tuple, or string
Function Name Description

Returns multiple random


elements from the list with
choices() replacement

Returns a particular length list of


sample() items chosen from the sequence

Generate random floating


random() numbers

Return a random floating number


between two numbers both
uniform() inclusive

Return a random floating point


number within a range with a bias
triangular() towards one extreme

Return a random floating point


betavariate() number with beta distribution

Return a random floating point


number with exponential
expovariate() distribution

gammavariate() Return a random floating point


number with a gamma
Function Name Description

distribution

Return a random floating point


gauss() number with Gaussian distribution

Return a random floating point


number with a log-normal
lognormvariate() distribution

Return a random floating point


normalvariate() number with normal distribution

Return a random floating point


number with von Mises
distribution or circular normal
vonmisesvariate() distribution

Return a random floating point


paretovariate() number with a Pareto distribution

Return a random floating point


weibullvariate() number with Weibull distribution

Saving and loading NumPy Arrays


The savetxt() and loadtxt() functions in NumPy are primarily designed for
1D and 2D arrays (text files with row/column format). When dealing with a
3D NumPy array, these functions can be a bit limited because they cannot
directly handle the 3D structure. However, you can reshape the 3D array
into a 2D array, save it, and then reshape it back to its original form upon
loading. In this article, we will see how to load and save 3D NumPy
Array to file using savetxt() and loadtxt() functions and NumPy loadtxt and
savetxt usage guide.
Load and Save 3D Numpy Array to File
Below are the ways by which we can load and save 3D NumPy array to file
using savetxt() and loadtxt() functions in Python:
 Utilize the savetxt() and loadtxt() functions for TXT files
 Saving and loading the 3D arrays(reshaped) into CSV files
Example 1: Saving a 3D Numpy Array as a Text File
In this example, a 3D NumPy array arr is reshaped into a 2D format and
saved to a text file named "[Link]" using savetxt(). Later, the data is
retrieved from the file, reshaped back to its original 3D form, and
compared with the original array to verify its equality.
import numpy as gfg

arr = [Link](5, 4, 3)

# reshaping the array from 3D


# matrice to 2D matrice.
arr_reshaped = [Link]([Link][0], -1)

# saving reshaped array to file.


[Link]("[Link]", arr_reshaped)

# retrieving data from file.


loaded_arr = [Link]("[Link]")

load_original_arr = loaded_arr.reshape(
loaded_arr.shape[0], loaded_arr.shape[1] // [Link][2], [Link][2])

# check the shapes:


print("shape of arr: ", [Link])
print("shape of load_original_arr: ", load_original_arr.shape)

# check if both arrays are same or not:


if (load_original_arr == arr).all():
print("Yes, both the arrays are same")
else:
print("No, both the arrays are not same")
Output:
shape of arr: (5, 4, 3)
shape of load_original_arr: (5, 4, 3)
Yes, both the arrays are same
Example 2: Saving and loading the 3D arrays(reshaped) into CSV files
In this example, we will perform saving and loading the 3D
arrays(reshaped) into CSV files by using savetxt and loadtxt functions
respectively. Here, a random 3D NumPy array arr is reshaped into a 2D
format, saved as a CSV file, and then loaded back into a 2D array. The
loaded data is reshaped back to its original 3D form, and a comparison is
made with the original array to confirm their equality.
import numpy as np

# Create a sample 3D array


arr = [Link](5, 4, 3)

# Reshape the 3D array to 2D


arr_reshaped = [Link]([Link][0], -1)

# Save the 2D array to a CSV file


[Link]("3d_array.csv", arr_reshaped, delimiter=",")

# Load the 2D array from the CSV file


loaded_arr = [Link]("3d_array.csv", delimiter=",")

# Reshape the 2D array back to its original 3D shape


load_original_arr = loaded_arr.reshape(([Link][0], [Link][1],
[Link][2]))

# Verify if the loaded array matches the original


if np.array_equal(load_original_arr, arr):
print("Yes, both the arrays are the same")
else:
print("No, both the arrays are not the same")
Output:
Yes, both the arrays are same

PANDAS INTRODUCTION

Pandas is an open-source Python library used for data manipulation,


analysis and cleaning. It provides fast and flexible tools to work with
tabular data, similar to spreadsheets or SQL tables.
Pandas is widely used in data science and analytics due to its integration
with libraries such as:
 NumPy: numerical operations
 Matplotlib and Seaborn: data visualization
 SciPy: statistical analysis
 Scikit-learn: machine learning workflows
Pandas allows efficient handling and analysis of data in a few lines of
code.
Installation
Before using Pandas, make sure it is installed:
pip install pandas
After the Pandas have been installed in the system we need to import the
library. This module is imported using:
import pandas as pd
Note: pd is just an alias for Pandas. It’s not required but using it makes the
code shorter when calling methods or properties.
Data Structures in Pandas
Pandas provides two data structures for manipulating data which are as
follows:
1. Pandas Series
A Pandas Series is one-dimensional labeled array capable of holding data
of any type (integer, string, float, Python objects etc.). The axis labels are
collectively called indexes. Series is created by loading the datasets from
existing storage which can be a SQL database, a CSV file or an Excel file.

import pandas as pd
import numpy as np
s = [Link]()
print("Pandas Series: ", s)
data = [Link](['g', 'e', 'e', 'k', 's'])

s = [Link](data)
print("Pandas Series:\n", s)
Output
Pandas Series
2. Pandas DataFrame
Pandas DataFrame is a two-dimensional data structure with labeled axes
(rows and columns). It is created by loading the datasets from existing
storage which can be a SQL database, a CSV file or an Excel file. It can be
created from lists, dictionaries, a list of dictionaries etc.
import pandas as pd
df = [Link]()
print(df)
lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']

df = [Link](lst)
print(df)
Output:
Pandas DataFrame

Operations in Pandas
Pandas provides essential operations for working with structured data
efficiently. The sections below introduce the most commonly used
functionalities with short explanations and simple examples.
1. Loading Data: This operation reads data from files such as CSV, Excel or
JSON into a DataFrame.

import pandas as pd
df = pd.read_csv("[Link]")
print([Link]())
Output

Output of Loading Dataset


Explanation: pd.read_csv("[Link]") reads the CSV file and loads it into a
DataFrame and [Link]() shows the first 5 rows of the data.
You can download the [Link] file from here

2. Viewing and Exploring Data: After loading data, it is important to


understand its structure and content. This methods allow you to inspect
rows, summary statistics and metadata.
print([Link]())
Output

3. Handling Missing Data: Datasets often contain empty or missing values.


Pandas provides functions to detect, remove or replace these values.
print([Link]().sum())
df = [Link](0)
Output
No Columns have NAN value
Explanation: [Link](0) replaces missing values with 0.
4. Selecting and Filtering Data: This operation retrieves specific columns,
rows or records that match a condition. It allows precise extraction of
required information.
ages = df[df['age'] > 25]
print(ages)
Output

Output of Filtering Data


Explanation df[df['age'] > 25] returns rows where the "age" value is
greater than 25.
5. Adding and Removing Columns: You can create new columns based on
existing ones or delete unwanted columns from the DataFrame.
df['total'] = df['a'] + df['b']
print([Link]())
Output

Adding new column "total"


Explanation: df['total'] = df['a'] + df['b'] creates a new column named
"total".
6. Grouping Data (GroupBy): Grouping allows you to organize data into
categories and compute values for each group for example, sums, counts
or averages.
res = [Link]('category')['sales'].sum()
print(res)
Output

Grouping Data
Explanation: [Link]('category') divides the dataset based on the
"category" column.
Creating a Pandas DataFrame
A Pandas DataFrame is a data structure for storing and manipulating data
in a table format (rows and columns), similar to Excel or SQL. It makes
handling, filtering and analyzing large datasets easy. A DataFrame can be
created using various data structures like lists, dictionaries, NumPy arrays
etc.
Creating an Empty DataFrame
An empty Pandas DataFrame is a table with no data, though it can have
defined columns or indexes. It’s useful for setting up a structure before
adding data and can be created using the DataFrame constructor.
import pandas as pd
df = [Link]()
print(df)

Output
Empty DataFrame
Columns: []
Index: []
Creating a DataFrame from a List
One way to create a DataFrame is by using a single list. Pandas
automatically assigns index values to the rows when you pass a list.
 Each item in the list becomes a row.
 The DataFrame consists of a single unnamed column.
import pandas as pd
lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']
df = [Link](lst)
print(df)

Output
0
0 Geeks
1 For
2 Geeks
3 is
4 portal
5 for
6 Geeks
Creating DataFrame from dict of Numpy Array
We can create a Pandas DataFrame using a dictionary of NumPy arrays.
Each key in the dictionary represents a column name and the
corresponding NumPy array provides the values for that column.
import numpy as np
import pandas as pd
data = { 'A': [Link]([1, 4, 7]),
'B': [Link]([2, 5, 8]),
'C': [Link]([3, 6, 9]) }
df = [Link](data)
print(df)

Output
A B C
0 1 2 3
1 4 5 6
2 7 8 9
Creating a DataFrame from a List of Dictionaries
We can create a DataFrame using a list of dictionaries, where each
dictionary represents a row. This is useful for handling structured data
from APIs or JSON, and is commonly used in web scraping and API
processing.
import pandas as pd
data = [
{'name': 'Mike', 'degree': 'MBA', 'score': 90},
{'name': 'Dan', 'degree': 'BCA', 'score': 40},
{'name': 'Emilia', 'degree': '[Link]', 'score': 80},
]
df = [Link](data)
print(df)

Output
name degree score
0 Mike MBA 90
1 Dan BCA 40
2 Emilia [Link] 80
Pandas Read CSV in Python
CSV files are Comma-Separated values files that allow storage of tabular
data.
 To access data from the CSV file, we require a function read_csv()
from Pandas that retrieves data in the form of the data frame.
 First, we must import the Pandas library, then using Pandas load this
data into a DataFrame
In the code below, we are working with a CSV file named [Link] which
contains people data.
import pandas as pd

df = pd.read_csv("[Link]")
df
Output
Creating DataFrame from dict of Numpy Array
We can create a Pandas DataFrame using a dictionary of NumPy arrays.
Each key in the dictionary represents a column name and the
corresponding NumPy array provides the values for that column.
import numpy as np
import pandas as pd

data = { 'A': [Link]([1, 4, 7]),


'B': [Link]([2, 5, 8]),
'C': [Link]([3, 6, 9]) }
df = [Link](data)
print(df)

Output
A B C
0 1 2 3
1 4 5 6
2 7 8 9
Creating a DataFrame from a List of Dictionaries
We can create a DataFrame using a list of dictionaries, where each
dictionary represents a row. This is useful for handling structured data
from APIs or JSON, and is commonly used in web scraping and API
processing.
import pandas as pd
data = [
{'name': 'Mike', 'degree': 'MBA', 'score': 90},
{'name': 'Dan', 'degree': 'BCA', 'score': 40},
{'name': 'Emilia', 'degree': '[Link]', 'score': 80},
]
df = [Link](data)
print(df)

Output
name degree score
0 Mike MBA 90
1 Dan BCA 40
2 Emilia [Link] 80

You might also like