0% found this document useful (0 votes)
30 views5 pages

Pandas Library

The document discusses various Pandas methods for working with Series and DataFrames. It shows how to create a Series with labels from a NumPy array, create a Series from a dictionary, and create an empty DataFrame. It also demonstrates the describe(), head(), tail(), and info() methods, providing sample code and output for each. These methods provide statistics, viewing rows, and information about a DataFrame.

Uploaded by

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

Pandas Library

The document discusses various Pandas methods for working with Series and DataFrames. It shows how to create a Series with labels from a NumPy array, create a Series from a dictionary, and create an empty DataFrame. It also demonstrates the describe(), head(), tail(), and info() methods, providing sample code and output for each. These methods provide statistics, viewing rows, and information about a DataFrame.

Uploaded by

none
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

20

WEEK-4 (Pandas Library)


a) Write a python program to implement Pandas Series with
labels.
Description:
Pandas’ series is a One-dimensional ndarray with axis labels. The
labels need not be unique but must be a hashable type. The object
supports both integer- and label-based indexing and provides a
host of methods for performing operations involving the index.
Code:
#To implement Pandas Series with Pandas Series with labels
# import pandas and numpy
import pandas as pd
import numpy as np
# creating simple array
name = np.array(["Meghu","Asri","Sreeja","Mahathi"])
date = pd.Series(name,index=[31,12,5,13])
# accessing a element using index element
print(date[12])
Sample Output:
Asri
b) Create a Pandas Series from a dictionary.

Description:
A Series is a one-dimensional labeled array capable of holding any
data type (integers, strings, floating point numbers, Python objects,
etc.). It has to be remembered that unlike Python lists, a Series will
always contain data of the same type. We can craete a panda series
from a dictionary using " series ( ) " method. In this case, dictionary
keys are taken in a sorted order to construct index.
21

Code:
#creating Panda Series from a dictionary
import pandas as pd
dictionary={'Meghana' : 31, 'Asritha' : 5, 'Sreeja' : 59, 'Mahathi' : 36}
series=pd.Series(dictionary)
print(series)
Sample Output:
Meghana 31
Asritha 5
Sreeja 59
Mahathi 36
dtype: int64

c) Creating a Pandas DataFrame.


Description:
A Dataframe is a two-dimensional data structure, i.e., data is
aligned in a tabular fashion in rows and columns. In dataframe
datasets arrange in rows and columns, we can store any number of
datasets in a dataframe. We can perform many operations on these
datasets like arithmetic operation, columns/rows selection,
columns/rows addition etc. Pandas DataFrame can be created in
multiple ways. A basic DataFrame, which can be created is an
Empty Dataframe. An Empty Dataframe is created just by calling a
dataframe constructor.
Code:
#Creating a Pandas dataframe
import pandas as pd
my_frnds={
'name' : ['meghu','sreeja','asri','mahathi'],
'no.' : [34,59, 5, 36],
'sec' : ['CSM','CSM','CSM','CSM'],
}
df = pd.DataFrame(my_frnds)
print(df)
22

Sample Output:
name no. sec
0 meghu 34 CSM
1 sreeja 59 CSM
2 asri 5 CSM
3 mahathi 36 CSM

d) Write a program which make use of following Pandas


methods
i) describe( )
ii) head( )
iii) tail( )
iv) info( )

Description:
describe( ) :-
This function describes different types of statstics like count,mean,
Standard deviation, minimal value, First Quartile, Second Quartile,
Third Quartile and maximum value.

Code:
#For describe function
import pandas as pd
date = [[31, 1, 2], [12, 5, 3], [5, 6, 3]]
df = pd.DataFrame(date)
print(df.describe())
23

Sample Output:
0 1 2
count 3.000000 3.000000 3.000000
mean 16.000000 4.000000 2.666667
std 13.453624 2.645751 0.577350
min 5.000000 1.000000 2.000000
25% 8.500000 3.000000 2.500000
50% 12.000000 5.000000 3.000000
75% 21.500000 5.500000 3.000000
max 31.000000 6.000000 3.000000

Description:
head( ) :- This method gives headers and a specified number of
rows starting from top. By default it returns top most 5 values.
Code:
#For head function
import pandas as pd
date = [[31, 1, 2], [12, 5, 3], [5, 6, 3],[1,3,4],[3,4,5],[67,98,8]]
df = pd.DataFrame(date)
print(df.head(3))
Sample Output:
0 1 2
0 31 1 2
1 12 5 3
2 5 6 3
Description:
tail( ) :- This method gives headers and a specified number of rows
starting from bottom. By default it returns bottom most 5 values.
24

Code:
#For tail function
import pandas as pd
date = [[31, 1, 2], [12, 5, 3], [5, 6, 3],[1,3,4],[3,4,5],[67,98,9,8]]
df = pd.DataFrame(date)
print(df.tail(2))
Sample Output:
0 1 2 3
4 3 4 5 NaN
5 67 98 9 8.0
Description:
info( ) :-This method returns the information like index, column
no., whether it is null or not ,count and datatype.
Code:
#For info function
import pandas as pd
date = [[31, 1, 2], [12, 5, 3], [5, 6, 3],[1,3,4],[3,4,5],[67,98,9,8]]
df = pd.DataFrame(date)
print(df.info())
Sample Output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 0 6 non-null int64
1 1 6 non-null int64
2 2 6 non-null int64
3 3 1 non-null float64
dtypes: float64(1), int64(3)
memory usage: 320.0 bytes
None

You might also like