Sunteți pe pagina 1din 4

Q.1 What is NumPy?

NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional


array object, and tools for working with these arrays.

It is the fundamental package for scientific computing with Python. It contains various features including
these important ones:
 A powerful N-dimensional array object
 Sophisticated (broadcasting) functions
 Tools for integrating C/C++ and Fortran code
 Useful linear algebra, Fourier transform, and random number capabilities

Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of
generic data.
Arbitrary data-types can be defined using Numpy which allows NumPy to seamlessly and speedily
integrate with a wide variety of databases.

Q.2 What is DATAFRAME in Python ?

Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure


with labeled axes (rows and columns). A Data frame is a two-dimensional data structure, i.e., data is aligned
in a tabular fashion in rows and columns. Pandas DataFrame consists of three principal components,
the data, rows, and columns.

We will get a brief insight on all these basic operation which can be performed on Pandas DataFrame :
Creating a Pandas DataFrame
In the real world, a Pandas DataFrame will be created by loading the datasets from existing storage, storage
can be SQL Database, CSV file, and Excel file. Pandas DataFrame can be created from the lists, dictionary,
and from a list of dictionary etc. Dataframe can be created in different ways here are some ways by which
we create a dataframe:
Creating a dataframe using List: DataFrame can be created using a single list or a list of lists.
# import pandas as pd
import pandas as pd

# list of strings
lst = ['Geeks', 'For', 'Geeks', 'is',
'portal', 'for', 'Geeks']

# Calling DataFrame constructor on list


df = pd.DataFrame(lst)
print(df)
Q.3 How to Create Numpy array in python?

Introduction

At the heart of a Numpy library is the array object or the ndarray object (n-dimensional array). You will
use Numpy arrays to perform logical, statistical, and Fourier transforms. As part of working with Numpy,
one of the first things you will do is create Numpy arrays. The main objective of this guide is to inform a
data professional, you, about the different tools available to create Numpy arrays.
There are three different ways to create Numpy arrays:

1. Using Numpy functions


2. Conversion from other Python structures like lists
3. Using special library functions

Using Numpy functions

Numpy has built-in functions for creating arrays. We will cover some of them in this guide.
Creating a One-dimensional Array
First, let’s create a one-dimensional array or an array with a rank 1. arange is a widely used function to
quickly create an array. Passing a value 20 to the arange function creates an array with values ranging
from 0 to 19.

import Numpy as np
array = np.arange(20)
array
python
Output:
array([0, 1, 2, 3, 4,
5, 6, 7, 8, 9,
10, 11, 12, 13, 14,
15, 16, 17, 18, 19])

Creating a Two-dimensional Array


Let's talk about creating a two-dimensional array. If you only use the arange function, it will output a
one-dimensional array. To make it a two-dimensional array, chain its output with the reshape function.
array = np.arange(20).reshape(4,5)
array
python
Output:
1
2
3
4
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])

Q.4 What is Array in Python?

Python Arrays:
In this article, you’ll learn about Python arrays, difference between arrays and lists, and how and
when to use them with the help of examples.

In programming, an array is a collection of elements of the same type.

Arrays are popular in most programming languages like Java, C/C++, JavaScript and so on. However, in
Python, they are not that common. When people talk about Python arrays, more often than not, they are
talking about Python lists. If you don't know what lists are, you should definitely check Python list article.

That being said, array of numeric values are supported in Python by the array module.

How to create arrays?

As you might have guessed from the above example, we need to import array module to create arrays. For
example:

import array as arr


a = arr.array('d', [1.1, 3.5, 4.5])
print(a)

Here, we created an array of float type. The letter 'd' is a type code. This determines the type of the array
during creation.
Q.5 Difference between LIST IN PYTHON vs NUMPY ARRAY in Python ?

Arrays and lists are both used in Python to store data, but they don't serve exactly the same purposes. They
both can be used to store any data type (real numbers, strings, etc), and they both can be indexed and
iterated through, but the similarities between the two don't go much further. The main difference between
a list and an array is the functions that you can perform to them. For example, you can divide an array by
3, and each number in the array will be divided by 3 and the result will be printed if you request it. If you
try to divide a list by 3, Python will tell you that it can't be done, and an error will be thrown.

Here's how it would work:

x = array([3, 6, 9, 12])
x/3.0
print(x)

In the above example, your output would be:

array([1, 2, 3, 4])

If you tried to do the same with a list, it would very similar:

y = [3, 6, 9, 12]
y/3.0
print(y)

It's almost exactly like the first example, except you wouldn't get a valid output because the code would
throw an error.

It does take an extra step to use arrays because they have to be declared while lists don't because they are
part of Python's syntax, so lists are generally used more often between the two, which works fine most of
the time. However, if you're going to perform arithmetic functions to your lists, you should really be using
arrays instead. Additionally, arrays will store your data more compactly and efficiently, so if you're storing
a large amount of data, you may consider using arrays as well.

S-ar putea să vă placă și