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

Pandas I Notes 06 - June 20

Boolean indexing helps select data from DataFrames using a boolean vector, where we create a DataFrame with a boolean index and access the data using boolean indexing. CSV files store data in a tabular format separated by commas that can be imported into a DataFrame using pandas read_csv() method to load the data from the CSV file into a DataFrame that can then be analyzed such as finding averages, highest scores, and more.

Uploaded by

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

Pandas I Notes 06 - June 20

Boolean indexing helps select data from DataFrames using a boolean vector, where we create a DataFrame with a boolean index and access the data using boolean indexing. CSV files store data in a tabular format separated by commas that can be imported into a DataFrame using pandas read_csv() method to load the data from the CSV file into a DataFrame that can then be analyzed such as finding averages, highest scores, and more.

Uploaded by

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

Boolean Indexing

Boolean indexing helps us to select the data from the


DataFrames using a boolean vector. We need a DataFrame with a
boolean index to use the boolean indexing.

• Create a DataFrame with a boolean index as a vector.


• Access the data using boolean indexing.
Boolean Indexing – Method 1
import pandas as pd
l1=[1,4,7]
l2=[6,3,5]
l3=[3,6,9]
d={'A':l1,'B':l2,'C':l3}
d1=pd.DataFrame(d, index=[True,False,True])
print("DataFrame: 1")
print(d1)
print("Rows with True index")
print(d1.loc[True])

Note: print(d1.loc[False] → print the rows with False index


Boolean Indexing – Method 2
import pandas as pd
l1=[1,4,7]
l2=[6,3,5]
l3=[3,6,9]
d={'A':l1,'B':l2,'C':l3}
d1=pd.DataFrame(d)
print("DataFrame: 1")
print(d1)
print(d1[[True,True,False]])

Note: print(d1.loc[False] → print the rows with False index


CSV File
A CSV is a comma separated values file, which allows data to be saved in
a tabular format like a spreadsheet or database. Files in the CSV format
can be imported and exported from progams that store data in tables such
as Microsoft Excel or Open Office.
In CSV files data fields are most separated or delimited by a comma and
individual rows are separated by newline. To create a CSV file, first choose
Notepad and open a new file. Then enter the data separating each value
with a comma and each row with a new line. Save the file with the
extension CSV. You can open the file using MS Excel or another
spreadsheet program.
Importing CSV file to DataFrame
(Reading from a CSV file to Dataframe)

11 13

14

12
Importing CSV file to DataFrame
(Reading from a CSV file to Dataframe)
Steps:
1. Create a CSV file in MS Excel with data as follows:
Marks scored by 5 students in 3 subjects
2. Write Python program to import the data into data frames.
3. Display the imported data.
Importing CSV file to DataFrame
(Reading from a CSV file to Dataframe)

Syntax for read_csv():

df=pd.read_csv(<Filepath>)
It loads data from excel file to Pandas dataframe.
Importing CSV file to DataFrame
To display all column labels
print(marks.columns)
To display one column label
print(marks.columns[1])
To display mean of a column
print(marks.iloc[:,1].mean())

Using iteration
for (col,colSeries) in marks.iteritems():
if col!="Name":
print(col,colSeries.mean())
Importing CSV file to DataFrame
Using iteration
Importing CSV file to DataFrame
Display all the attributes of the dataframe
Importing CSV file to DataFrame
Display the total marks, average marks of each student.

Note: You can display


the average rounded to
2 decimal points.

Find out the function


to do it.
Importing CSV file to DataFrame
Display the total marks, average marks of each student.
Importing CSV file to DataFrame
Display the highest score of each student.

Display the highest/lowest score of each subject.

You might also like