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

IP Imp Notes

Uploaded by

max12342732k
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)
14 views5 pages

IP Imp Notes

Uploaded by

max12342732k
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

Informatics Practices | XII | 2022-23

PANDAS:

• Statement to import pandas library-


import pandas as pd

• Series can be created with- list, nd array, scalar, tuple

• DataFrame can be created with-


Series, list, dictionary, a numpy 2D array

• Pandas installation- pip install pandas

• Dictionary- {key:value, key:value}


• Add a column-
a) df[‘column name’] = [list of values]
b) df[“column name’]=values (same value for all rows)

• Remove a column-
df=df.drop(“column_name”,axis=1) 0r df=df.drop([“col1”,”col2”],axis=1)
or
df.pop(‘column name’)
or
del df[‘column name’]

• Add a row-
a) df.loc[row_label] = [list of values] # dataframe having labelled index
b) df.at[row_label]=values (same value for all columns)
c) df.loc[len(df.index)]=values (same value for all columns) # dataframe having
integer index

• Remove a row-
df=df.drop(row_index,axis=0)
df=df.drop( [0, 2, 4] )
df=df.drop( ‘R101’ ) # labelled index
• Selection of rows using loc –
df.loc['R105’]
df.loc["R102":"R104" # range of rows
df.loc[["R102","R104" ]]

• Selection of rows using iloc-


df.iloc[2]
df.iloc[0:3]
df.loc[[1,4]]

• Column Selection using loc-


df ['one'] # single column
df.one # single column
df[['Name','Class']] # columns not in range
df.loc[ : , "TMarks"]
df.loc[ : ,"Name" : "TMarks"]
• Column Selection using iloc-
df.iloc[:,col_index]
df.iloc[:,0:2] # first & second columns
df.iloc[: ,[0,2]] # first & last columns

• Selection of rows & columns using loc -


df.loc['R105’ , “Name”] # Row R105 with Name column
df.loc["R102":"R104" , “Roll” : “City” ] # Rows from R102 to R104 with Roll to
City column
df.loc[["R102","R104" ] , [“Roll”,”City”]] # Rows R102 & R104 with “Roll” & City
column
Selection of rows & columns using iloc-
df.iloc[2 , 1] # Row with index 2 & 2nd column
df.iloc[0:3 , 1:3] # Rows with index from 0 to 2 & columns from 2nd to 3rd
df.loc[[1,4] , [0,2]] # Rows with index 1 & 4 and columns 1st & 3rd.

• Selection by condition
df[df['Per'] > 80] # display those rows with all columns where Per >80
df[df['Per'] == 80] # display those rows with all columns where Per=80
df [ df[“Per”]>80].Name # display Name column where Per >80
df [ df[“Per”]>80][[“Name”,”Class”]] # display Name & Class columns where Per >80
df [ df[“Per”]>80][“Per”]+5

• df.head()- first 5 rows or df.head(3) first 3 rows

• df.tail()- last 5 rows or df.tail(3) last 3 rows

• difference between series and dataframe-


SERIES DATAFRAME
- 1D array - 2D array
- Homogenous - Heterogeneous
- Size is immutable - Size is mutable
• Difference between loc and iloc
LOC ILOC

- Includes strings and integer -includes only Integer.

- Includes the last value. –gives output excluding the last value

• To read a csv file- read_csv()


data = pd.read_csv("D: \export_ dataframe.csv")

• To export data from DataFrame to csv- to_csv()


df.to_csv ('D:\export_dataframe.csv', index = False, header=True)
• Rename columns
df.rename(columns= {"A" : "a" , "B" : "c"})
old new old new
• Rename index
df.rename(index= {0 : "a" , 1 : “b”, 2: "c"})
old new old new

• Shape of DataFrame-
(no.of rows, no.of columns)

•Size of DataFrame- Total rows x total column


DATA VISUALIZATION:

• Statement to import matplot-


import matplotlib.pyplot as plt

• To create a line chart- plt.plot(x,y)

• Syntax of a line chart-


import matplotlib.pyplot as plt
x=[values]
y=[values]
plt.xlabel()
plt.ylabel()
plt.title()
plt.plot(x,y)
plt.show()

• To create a bar graph- plt.bar(x,y)

• Syntax of a bar graph-


import matplotlib.pyplot as plt
x=[values]
y=[values]
plt.bar(x,y)
plt.xlabel()
plt.ylabel()
plt.title()
plt.show()

• To create a horizontal bar chart- plt.barh(x,y)

• To create a histogram-
import matplotlib.pyplot as plt
x=[values]
y=[values]
plt.hist(x,bins=y)
plt.xlabel()
plt.ylabel()
plt.title()
plt.legend()
plt.show()

• Default value of no.of bins in an histogram is 5


• A legend is an area describing the elements of a graph.
MY SQL:
Functions – set of pre defined commands.
Types :
1. Single row function – Maths , String & Date & time
2. Aggregate function

• Aggregate functions-

a) sum()- select sum(age);


b) min()- select min(marks);
c) max() – select max(price);
d) avg()- select avg(height);
e) count(*) – total no of records in a table
f) count(roll_no) – total no of values in a column

Note : count(*)gives ouput including Null value, and count(‘column name’) excluding Null values.
• Math functions-
a) pow()-pow(2,4)=16 (to the power)
b) mod()-mod(4,2)=0 (gives remainder)
c) round()-round(25.647,2)=25.65 (rounds off the values)
d) round()- select round(123.56,-1) = 120
• String functions-

a) length()-length(“kv bkp afs”)=10 (no.of characters)


b) concat()-concat(“hello”,”ip”)=helloip (combines)
c) lcase()-lcase(“hello IP”)= hello ip (lower case)
d) ucase()-ucase(“hello IP”)=HELLO IP (upper case)
e) trim()-trim(“ ip ”)=ip (removes spaces from both sides)
f) ltrim()-ltrim(“ ip ”)=ip (removes left space)
g) rtrim()rtrim(“ ip “)= ip(removes right space)
h) left()-left(“study well”,3)=stu (characters from left)
i) right()-right(“hello”,4)=ello (characters from right)
j) instr()-instr(“ip and cs”,”cs”)=8 (position of second string in the first string)
k) mid()/substr()-mid(“welcome”,4,4)=come (extracts characters)

• Date/time functions-
a) dayname()-(‘2022-01-20’)=thursday
b) dayofyear()-(‘2022-01-20’)=20
c) dayofmonth()-(‘2022-01-20’)=20
d) dayofweek()-(‘2022-01-20’)=5
e) monthname()-(‘2022-01-20’)= January
f) date()-(‘2022-01-20')=20 month()-(‘2022-01-20’)=01
g) year()-(‘2022-01-20’)=2022
h) now()=2022-01-20 10.35.4 (current date and time)
 Clause- group by, having, order by, where
 GROUP BY
 Group by clause helps up to divide the records table into logical groups based on any column
value.
 In those logically divided group of records we can apply aggregate functions.

Ex: select deptno, count(deptno) from employee group by deptno;

HAVING with GROUP BY


 To filter or restrict some records from the output produced by GROUP BY, HAVING clause is
used.
 It is used to put condition on group of rows.
 With having clause we can use aggregate functions also.

Example:
1. select deptno, sum(sal) from employee group by deptno having deptno in(10,20);

2. select deptno, count(empno) from employee group by deptno having count(*) > 1;

Difference between having & where clauses:

SORTING IN SQL—ORDER BY
 The SQL ORDER BY clause is used to sort the data in ascending or descending order based on
one or more columns.
 The ORDER BY keyword is used to sort the result-set by one or more fields in a table.
 This clause sorts the records in the ascending order (ASC) by default.
 To sort the records in descending order, DESC keyword is to be used.
Ex:
select roll,name from student order by name;
Select roll , name , marks from student order by marks desc;

Difference between order by & group by:


Order by Group by
Arranges or sort data in ascending or descending Divide the records of table in a group based on
order. column value
To give condition where is used. To give condition having clause is used.
Ex: select roll, name from student order by name; Ex: select gender , count(roll), name from student
group by gender;

You might also like