0% found this document useful (0 votes)
3 views

Lists and dictionary

Notes for ip class 12

Uploaded by

schoolvaishnavi5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lists and dictionary

Notes for ip class 12

Uploaded by

schoolvaishnavi5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

INFORMATICS PRACTICES (065) - XII BY G SIVA PRASAD

DataFrames
1. DataFrame:
 A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows
and columns.
 In DataFrame both data and size is mutable.

2. Features of DataFrame
 Potentially columns are of different types
 Size – Mutable
 Labeled axes (rows and columns)
 Can Perform Arithmetic operations on rows and columns

3. Structure of DataFrame
Let us assume we have a DataFrame called „student‟ whose structure is follows.

4. Creating and displaying DataFrame


 Syntax to create DataFrame

DataFrameName=pandas.DataFrame(data,index,columns,dtype)
Where,
 ‘data’ takes various forms like ndarray, series, lists, dict, constants and also another
DataFrame.
 ‘Index’ represents row labels.
 ‘columns’ represents column ``labels.
 ‘dtype’ represents data type of values.

DataFrame
12
INFORMATICS PRACTICES (NEW)-XI BYG SIVA PRASAD
(PGT)
 DataFrame mainly created by using the following 4 ways.
(a) DataFrame creation using list of list
(b) DataFrame creation using list of dictionary
(c) DataFrame creation using dictionary of list
(d) DataFrame creation using dictionary of Series
Note: as per our syllabus , we have only (b) and (d)

Eg: Write a python code to create the following DataFrame df1.


Pno Pname Price
r1 p1 Soap 5.0
r2 p2 Shampoo 5.5
r3 p3 Pen 6.0
r4 p4 Pencil 6.5
(a) DataFrame creation using list of list

(b) DataFrame creation using list of dictionary

DataFrame 13
INFORMATICS PRACTICES (NEW)-XI BYG SIVA PRASAD
(PGT)
(c) DataFrame creation using dictionary of list

(d) DataFrame creation using dictionary of Series

5. DataFrame Operations
 We can perform the following operations on DataFrame.
(a) Obtaining single column.
(b) Obtaining multiple columns.
(c) Obtaining single row.
(d) Obtaining multiple rows.
(e) Obtaining sequence of rows.
(f) Obtaining specific rows and columns.
(g) Adding a column.
(h) Modifying a column.
(i) Deleting a column.
(j) Adding a row.
(k) Modifying a row.
(l) Deleting a row.
(m) Accessing individual data item.
(n) Changing individual data item.

DataFrame 14
INFORMATICS PRACTICES (NEW)-XI BYG SIVA PRASAD
(PGT)

(a) Obtaining single column.


 Syntax to select a column from existing DataFrame
DataFrameName[columnname]
(or)
DataFrameName.columnname

 Eg (1): Write a Python statement to select a column called „Pname‟ from df1 DataFrame.
Ans:

(b) Obtaining multiple columns.

 Syntax to select a column from existing DataFrame

DataFrameName[[columnname1,colname2,…]]

 Eg (2): Write a Python statement to select a column called „Pname‟ and „Pno‟ from df1
DataFrame.
Ans:

(c) Obtaining single row.


 Syntax:

DataFrameName.Ioc[rowname]
(or)
DataFrameName.iIoc[rowindex]

 To get rows, we have to use either loc or iloc methods.


 loc will be used to get row(s) from DataFrame if row labels are string.
 iloc will be used to get row(s) from DataFrame if row labels are integers.
Note-1: loc method can be used to get rows from DataFrame if has only integer row labels.
Note-2: The single row result will be displayed in the form Series.

DataFrame 15
INFORMATICS PRACTICES (NEW)-XI BYG SIVA PRASAD
(PGT)
Eg(1): Write a Python statement to select a row whose label is r1 from the following df1.

Ans:

Eg(2): Write a Python statement to select a row whose label index is 0 from the above df1.
Ans:

(d) Obtaining multiple rows.


 Syntax:
DataFrameName.Ioc[[rowname1,rowname2,….]]
(or)
DataFrameName.iIoc[[rowintindex1,rowintindex2,….]]

 Eg1: Write a Python statement to display „r1‟ and „r3‟ rows from above DataFrame df.
Ans:

(e) Obtaining sequence of rows.


 Syntax:

DataFrameName.Ioc[stratingrowname:endingrowname]
(or)
DataFrame.iloc[strartingintindex:endingintindex]

DataFrame 16
INFORMATICS PRACTICES (NEW)-XI BYG SIVA PRASAD
(PGT)
Eg(1): Write a python code to siplay 2nd , 3rd and 4th rows from DataFrame df
Ans-1:

Ans-2:

(f) Obtaining specific rows and columns.


 Syntax:

DataFrameName.Ioc[stratingrowname:endingrowname,startingcolname:endingcolumnname]
(or)
DataFrame.iloc[rowstrartingintindex:rowendingintindex,columnstartingintindex:columnendingintindex]

 Eg: Write a python code to display the following output from above DataFrame df.

Ans:

(g) Adding a column to DataFrame


 Syntax to add a column to existing DataFrame
DataFrameName[columnname]=Value

Eg(1): Write a python code to add a new column called qty with [15,16,17,18] as values to the above
DataFrame df.
Ans:

DataFrame 17
INFORMATICS PRACTICES (NEW)-XI BYG SIVA PRASAD
(PGT)

Eg (2): In this example, we have created a DataFrame from dictionary initially, and then we added a new
column called „address‟ to the DataFrame „DF2‟.
import pandas as pd
importnumpyasnp
d1={'name':['ram','peter','Faiz'],'Rno':[1,2,3],'phno':[123,456,np.NaN]}
DF2=pd.DataFrame(d1)

print(DF2)
DF2['address']='jpnagar' Here, we added new column ‘address’ to DF2.
print('after adding a column the DF2 dataFrame is as follows')
print(DF2)

Output:
name Rno phno
0 ram 1 123.0
1 peter 2 456.0
2 Faiz 3 NaN

after adding a column the DF2 dataFrame is as follows

name Rno phno address


0 ram 1 123.0 jpnagar
1 peter 2 456.0 jpnagar
2 Faiz 3 NaN jpnagar

(h) Modifying a column.


Eg: Write python code to modify column Price with [7.00,7.50,8.00,8.50] values
Ans:

(i) Deleting a column from DataFrame


 Syntax to delete a column from existing DataFrame
del DataFrameName[columnname]
(or)
dfname.drop(columnname,axis=1,inplace=True)
DataFrame 18
INFORMATICS PRACTICES (NEW)-XI BYG SIVA PRASAD
(PGT)
Eg (1): Write a python statement to delete a column called „qty‟ from DataFrame „df‟.
Ans;

Note: In above example, axis=1 represents column. Actually default axis values is 0(zero)
which represents row,
(j) Adding a row.
 loc method is used to add/modify a row in DataFrame.
 Eg(1): Write a python code to add new row with ‘r5’ as row label and
[„p5‟,‟Notebook‟,7.00] as values.
Ans:

 Eg(2):In the following example, „hi‟ is common for all the columns in that row.

(k) Modifying a row.


Eg(1): In the following example, we modified the row whose index is „r6‟.

DataFrame 19
INFORMATICS PRACTICES (NEW)-XI BYG SIVA PRASAD
(PGT)

(l) Deleting a row.


 Row(s) can be deleted by using drop() function.
 Syntax:

DataFrameName.drop(„rowindex‟,inplace=True)
(or)
DataFrameName.drop(„rowindex‟,axis=0,inplace=True)

 Write a python code to delete a row whose label is ‘r6’.


Ans:

(m) Accessing individual data item.


Syntax to select individual data from existing DataFrame

DataFrameName.coIumnname[rowname]
(or)
DataFrameName.at[rowname,columnname]
(or)
DataFrameName.iat[rowindex,columnindex]
(or)
DataFrameName.loc[rowname,columnname]
(or)
DataFrameName.iloc[rowindex,columnindex]

DataFrame 20
INFORMATICS PRACTICES (NEW)-XI BYG SIVA PRASAD
(PGT)
 Eg: Write a python code to display only shampoo from the following DataFrame df.

Ans:

(n) Changing individual data item.


Syntax: to change individual data from existing DataFrame

DataFrameName.at[rowname,columnname]
=newvaIue (or)
DataFrameName.iat[rowindex,columnindex] =newvaIue
(or)
DataFrameName.loc[rowname,columnname] =newvaIue
(or)
DataFrameName.iloc[rowindex,columnindex] =newvaIue

Eg: Write a python code to change Shampoo with Conditioner.

Ans:

DataFrame 21
INFORMATICS PRACTICES (NEW)-XI BYG SIVA PRASAD
(PGT)

6) Iteratinq over a DataFrame


We have the following methods to iterate a DataFrame over column/rows . They are:
(a) iterrows()
(b) iteritems()
6.a. iterrows()
iterrows() returns the iterator yielding each index value along with a series containing the data in
each row.

 The iterrows() will return output in the form of (row_index, Series).

Eg (1):

6.b. iteritems()
 Iterates over each column as key, value pair with label as key and column value as a Series object.
 The iteritems() will return output in the form of (column_index, Series).

DataFrame 22
INFORMATICS PRACTICES (NEW) - XI BY G SIVA PRASAD (PGT)

Eg (4): This example, illustrates about mul() operation.

Eg (1):

7. DataFrame Attributes:
Some common attributes of DataFrame objects are:
Attribute Meaning
DataFrame.index The index of the DataFrame
DataFrame.columns Column labels of the DataFrame
DataFrame.axes Returns both indexes and column names
DataFrame.dtype Return data type of data
DataFrame.shape Return a tuple of the shape
DataFrame.nbytes Return the number of bytes occupied by data
DataFrame.ndim Return the number of dimensions
DataFrame.size Return the number of elements
DataFrame.hasnans Return DataFrame if Series has NaN values, otherwise False
DataFrame.empty Return DataFrame if Series object is empty, otherwise False

DataFrame 23
INFORMATICS PRACTICES (NEW) - XII BY G SIVA PRASAD
(PGT)
Eg (1): Consider the following DataFrame df.

8) head() and tail() functions

 head(n) function is used to get first “n‟ rows from DataFrame.


 head(-n) function will return all rows except n rows from ending.
 If we don‟t supply parameter to head() function, then it will return first 5 rows from DataFrame.
 Consider the following DataFrame df.

24
INFORMATICS PRACTICES (NEW) - XII BY G SIVA PRASAD
(PGT)
 Eg(1):

 Eg(2):

 Eg(3):

 Eg(4):

Note: In above eg(4) , except last three remaining all rows will come as an output.
 tail(n) function is used to get last “n‟ rows from DataFrame.
 tail(-n) function will return all rows except n rows from beginning.
 If we don‟t supply parameter to tail() function, then it will return last 5 rows from DataFrame.

25
INFORMATICS PRACTICES (NEW) - XII BY G SIVA PRASAD
(PGT)
 Eg(1):

 Eg(2):

 Eg(3):

9) Boolean Indexing:
 Setting boolean values(True/False/1/0) as indexes in DataFrame is called boolean indexing.
 Boolean indexing is defined as a very important feature of numpy, which is frequently used in
pandas.
 Its main task is to use the actual values of the data in the DataFrame.
 We can filter the data in the boolean indexing in different ways, which are as follows:
 Accessing a DataFrame with a boolean index
 Applying a boolean mask to a DataFrame
 Masking data based on column value
 Masking data based on an index value

26
INFORMATICS PRACTICES (NEW) - XII BY G SIVA PRASAD
(PGT)

Eg:

Output:

10) renaming row labels and column labels


 rename() function is used to rename either row labels or column labels;
 Syntax:

dfname.rename(index={oldindex:newindex,…..},columns={oldname:newname,…..},inplace=True)

 Eg: Consider the following DataFrame df and rename row index r1 as row1 and column label pname
as prodname

Ans:

27
INFORMATICS PRACTICES (NEW) - XII BY G SIVA PRASAD
(PGT)
11) DATA TRANSFER BETWEEN CSV FILES AND DATAFRAME OBJECTS
1. Transferring data from .csv files to DataFrames
 The acronym for CSV is Comma Separated Values.
 CSV is asimple fileformat used tostoretabular data, suchas aspreadsheet or database.
 Files in the CSV format can be imported to and exported from programs that store data in tables,
such as Microsoft Excel or OpenOffice Calc.
 Advantages of .csv files
• Simple and compact for data storage.
• A common format for data interchange.
• It can open in spreadsheets.
 By using read_csv() method we can read and convert into DataFrame.
 Syntax to transfer .csv file to DataFrame

DataFrame_Name=pandas.read_csv(“path\\filename.csv”)

Eg(1): Write a python code to convert “simple.csv” which is in “D” drive “IP” folder into a DataFrame
called “DF1”.

2.Transferring data from DataFrames to .csv files


 By using to_csv() function we can convert a DataFrame into .csvfile.
Synatx:

DataFrame_Name.to_csv(“path\\filename.csv”)

 The DataFrame will be converted and stored in the specified path with given name.

28

You might also like