IP Imp Notes
IP Imp Notes
PANDAS:
• 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 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
- Includes the last value. –gives output excluding the last value
• Shape of DataFrame-
(no.of rows, no.of columns)
• 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()
• Aggregate functions-
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-
• 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.
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;
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;