Professional Documents
Culture Documents
import pandas as pd
import numpy as np
# Create a Series
data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
series = pd.Series(data)
percentile_75 = series.quantile(0.75)
above_percentile_75 = series[series > percentile_75]
output
3. Create a Data Frame quarterly sales where each row contains the item
category, item name, and expenditure. Group the rows by the category and
print the total expenditure per category.
import pandas as pd
data = {
'Category': ['A', 'B', 'A', 'B', 'C'],
'Item Name': ['Item1', 'Item2', 'Item3', 'Item4', 'Item5'],
'Expenditure': [100, 150, 200, 120, 80]
}
df = pd.DataFrame(data)
total_expenditure = df.groupby('Category')['Expenditure'].sum()
print("Total expenditure per category:")
print(total_expenditure)
output
Total expenditure per category:
Category
A 300
B 270
C 80
Name: Expenditure, dtype: int64
4. Create a data frame for examination result and display row labels,
column labels data types of each column and the dimensions
import pandas as pd
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Math': [85, 70, 92],
'Science': [90, 88, 78]
}
df = pd.DataFrame(data)
data = {
'Name': ['Alice', 'Bob', 'Alice', 'Charlie', 'Bob'],
'Score': [85, 70, 85, 92, 70]
}
df = pd.DataFrame(data)
output
DataFrame without duplicate rows:
Name Score
0 Alice 85
1 Bob 70
3 Charlie 92
import pandas as pd
# Export DataFrame to CSV file
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 28]}
df = pd.DataFrame(data)
df.to_csv('output.csv', index=False)
Output
Imported DataFrame from CSV:
Name Age
0 Alice 25
1 Bob 30
2 Charlie 28
import pandas as p1
import numpy as np1
data = np1.array(['a','b','c','d'])
s = p1.Series(data,index=[100,101,102,103])
print(s)
Output
100 a
101 b
102 c
103d dtype:
Object
Output:
3 Kavi
5 Shyam
1 Ravi
dtype: object
Output:
01
12
23
34
dtype: int32
(C) Creation of Series from Dictionary
Dictionary keys can be used to construct an index for a
Series . Here, keys of the dictionary dict1 become indices in the series.
Output:
India NewDelhi
UK London
Japan Tokyo
dtype: object
9)
Pandas Series
Head function
10)
Pandas Series
tail function
Output
Name Age
1 Freya 9
2 Mohak 10
Output
xyz
0 1 2 NaN
1 5 4 5.0
14)Row Selection, Addition, and Deletion
#Selection by Label
import pandas as pd1
d1 = {'one' : pd1.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd1.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df1
= pd1.DataFrame(d1)
print (df1.loc['b'])
Output
one 2.0
two 2.0
Name: b, dtype: float64
Output
one 3.0
two 3.0
Name: c, dtype: float64
16)Iterate over rows in a dataframe
Output
freya 10
mohak 1
17)Binary operation over dataframe with series
import pandas as pd
x = pd.DataFrame({0: [1,2,3], 1: [4,5,6], 2: [7,8,9] })
y = pd.Series([1, 2, 3])
new_x = x.add(y, axis=0)
print(new_x)
Output 0 1 2
0147
1 4 10 16
2 9 18 27
18)Binary operation over
dataframe with dataframe
import pandas as pd
x = pd.DataFrame({0: [1,2,3], 1: [4,5,6], 2: [7,8,9] })
y = pd.DataFrame({0: [1,2,3], 1: [4,5,6], 2: [7,8,9] })
new_x = x.add(y, axis=0)
print(new_x)
Output
012
0 2 8 14
1 4 10 16
2 6 12 18
19)Merging/joining dataframe
import pandas as pd
left = pd.DataFrame({
'id':[1,2],
'Name': ['anil', 'vishal'],
'subject_id':['sub1','sub2']})
right = pd.DataFrame(
{'id':[1,2],
'Name': ['sumer', 'salil'],
'subject_id':['sub2','sub4']})
print (pd.merge(left,right,on='id'))
Output1
input
import matplotlib.pyplot as pp
day
=['Monday','Tuesday','Wednesday','Thursday','Friday']
inc = [510,350,475,580,600]
pp.plot(day,inc,label='Income',color='r',linestyle='d
ashed',marker='D')
pp.title("The Weekly Income Report")
pp.xlabel("Days")
pp.ylabel("Income")
pp.legend()
pp.show()
Output:
2) A Shivalik restaurant has recorded the following data into their register
for their income by Drinks and Food. Plot them on the line chart.
Day Monday Tuesday Wednesday Thursday Friday
input
import matplotlib.pyplot as pp
day
=['Monday','Tuesday','Wednesday','Thursday','Friday']
dr = [450,560,400,605,580]
fd = [490,600,425,610,625]
pp.plot(day,dr,label='Drinks',color='g',linestyle='do
tted',marker='+')
pp.plot(day,fd,label='Food',color='m',linestyle='dash
dot',marker='x')
pp.title("The Weekly Restaurant Orders")
pp.xlabel("Days")
pp.ylabel("Orders")
pp.legend()
pp.show()
Output:
3) Write a program to plot a range from 1 to 30 with step value 4. Use
the following algebraic expression to show data.
y = 5*x+2
import matplotlib.pyplot as pp
import numpy as np
x = np.arange(1,30,4)
y = 5 * x + 2
pp.plot(x,y)
pp.show()
Output:
4)Display following bowling figures through bar chart:
Overs Runs
1 6
2 18
3 10
4 5
import matplotlib.pyplot as pp
overs =[1,2,3,4]
runs=[6,18,10,5]
pp.bar(overs,runs,color='m')
pp.xlabel('Overs')
pp.xlabel('Runs')
pp.title('Bowling Spell Analysis')
pp.xticks([1,2,3,4])
pp.yticks([5,10,15,20])
pp.show()
Output:
5)Given the school result data,analyse the performance of 5 students on
different parameter, e.g. subject wise.
Below is implementation code /source code
Here is program code to analyse the performance of student data on
different parameter, e.g. subject wise or class wise
Below is output:
******************Marksheet****************
English Maths IP Chemistry Biology
Sumedh 67 55 66 45 54
Athang 89 67 78 56 65
Sushil 90 45 89 67 76
Sujata 55 56 90 65 87
ii)insert 5 values:
Input:
INSERT INTO student values(1, 'John', 85),
(2, 'Jane', 75),
(3, 'Michael', 90),
(4, 'Emma', 95),
(5, 'William', 78);
Input:
INSERT INTO student (student_id, name, marks) VALUES (6, 'Olivia',
88);
Output: 1 new record is inserted
3. Delete the details of a student in the above table.
Input:
DELETE FROM student WHERE student_id = 3;
4. Use the select command to get the details of the students with marks
more than 80.
Input:
SELECT * FROM student WHERE marks > 80;
Output:
| student_id | name | marks |
|------------|----------|-------|
| 1 | John | 85 |
| 3 | Michael | 90 |
| 4 | Emma | 95 |
| 6 | Olivia | 88 |
5. Find the min, max, sum, and average of the marks in a student
marks table.
Input:
SELECT MIN(marks) AS min_marks, MAX(marks) AS max_marks, SUM(marks) AS
sum_marks, AVG(marks) AS avg_marks
FROM student;
Output:
| min_marks | max_marks | sum_marks | avg_marks |
|-----------|-----------|-----------|-----------|
| 75 | 95 | 423 | 84.6 |
Output:
|---------------|--------------|------|-----|---------|----------------|
Input:
INSERT INTO customers VALUES(1, 'Alice', 'USA'),
(2, 'Bob', 'Canada'),
(3, 'Charlie', 'USA'),
(4, 'David', 'UK'),
(5, 'Eva', 'Canada'),
(6, 'Frank', 'Germany');
Output:
v)Find the total number of customers from each country using GROUP
BY:
Input:
SELECT country, COUNT(*) AS num_customers FROM
customers GROUP BY country;
Output:
| country | num_customers |
|---------|---------------|
| Canada | 2 |
| Germany | 1 |
| UK |1 |
| USA | 2 |
Input:
SELECT student_id, marks
FROM student
ORDER BY marks DESC;
Output:
| student_id | marks |
|------------|-------|
|4 | 95 |
|3 | 90 |
|6 | 88 |
|1 | 85 |
|5 | 78 |
|2 | 75 |