**Create a Series based on given data using the following methods:
data = [7,8,9,10] index = ['A','B','C','D']
Using a list
Using a Dictionary
import pandas as pd
s = pd.Series(data = [7,8,9,10], index = ['A','B','C','D'])
print(s)
A 7
B 8
C 9
D 10
dtype: int64
import pandas as pd
data = {'A':7, 'B':8, 'C':9, 'D':10}
s = pd.Series(data)
print(s)
A 7
B 8
C 9
D 10
dtype: int64
import pandas as pd
d = {1:10, 2:20}
s = pd.Series(d, index = ['A', 'B'])
print(s)
A NaN
B NaN
pw: yadhu
dtype: float64
**Create a Series of a Scalar Value 10 with the following index:
index = ['M','N','O','P']
import pandas as pd
s = pd.Series(10, index = ['M','N','O','P'])
print(s)
M 10
N 10
O 10
P 10
dtype: int64
**Create a Series of even upto 20 with default indices using range function
import pandas as pd
s = pd.Series(range(2, 21, 2))
print(s)
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
9 20
dtype: int64
import pandas as pd
s = pd.Series([x for x in range(2,21,2)])
print(s)
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
9 20
dtype: int64
**Create a Series of numbers and print its attributes
shape
size
nybtes
dtype
hasnans
index
values
empty
Properties of Series object
s = pd.Series(data=[10,20,30,40], index=['A','B','C','D'], dtype='int8')
print("The type of data:", s.dtype)
print("The number of rows and columns", s.shape)
print("The number of bytes:", s.nbytes)
print("The size of the Series or number of elements:", s.size)
print("To check if the Series is empty:", s.empty)
print("To check if the Series has NAN Values:", s.hasnans)
print("To check the values:", s.values)
print("To check the index:", s.index)
The type of data: int8
The number of rows and columns (4,)
The number of bytes: 4
The size of the Series or number of elements: 4
To check if the Series is empty: False
To check if the Series has NAN Values: False
To check the values: [10 20 30 40]
To check the index: Index(['A', 'B', 'C', 'D'], dtype='object')
Practical 2
**Create a Series of numbers with alphabetic indices and perform the operations
[10,20,30,40,50] with index ['M','N','O','P','Q']
Display the slice 10 30 50 using numerical indices
Display the slice 50 30 10 using numerical indices
Display the slice 10 20 30 using labeled slice
Display the slice of all the elements that are greater than 30
Use reindex operation and rearrange all the elements without NaNs
import pandas as pd
s = pd.Series(data = [10,20,30,40,50], index = ['M','N','O','P','Q'])
print(s)
M 10
N 20
O 30
P 40
Q 50
dtype: int64
import pandas as pd
s = pd.Series([10,30,50])
print(s)
0 10
1 30
2 50
dtype: int64
import pandas as pd
s = pd.Series([50,30,10])
print(s)
0 50
1 30
2 10
dtype: int64
import pandas as pd
s = pd.Series([10,20,30], ['A','B','C'])
print(s)
print("___")
print(s[s>30])
A 10
B 20
C 30
dtype: int64
Series([], dtype: int64)
import pandas as pd
s = pd.Series([10,20,30], ['A','B','C'])
t = s. reindex (['B', 'A', 'C'])
print(t)
B 20
A 10
C 30
dtype: int64
import pandas as pd
df = pd.read_csv(r'C:\Users\acer\Documents\data.csv')
print(df)
roll no name class age
0 1001 TOM X 16
1 1002 BOB XI 17
2 1003 SID XI 17
ayush.txt
import pandas as pd
df = pd.read_csv(r'D:\ayush.txt')
print(df)
I LOVE INDIA
0 i live in india
1 INDIA HAS MANY STATES
1
**Create the following Series using reindex() method
[10.20.30.40,40] with indexs ['A','B','C','D','E']
reindex with ['A','B','F','D','G']
)
1
import pandas as pd
2
s = pd.Series(data=[10,20,30,40,50], index = ['A','B','C','D','E'])
3
d = s.reindex(['A','B','F','D','G'])
4
print(d)
A 10.0
B 20.0
F NaN
D 40.0
G NaN
dtype: float64
Create a dataframe using the following information
Using a nested list
using a dictionary of list
Using a nested dictionary
Using a Dictionary of Series
1
import pandas as pd
2
l = [[10,True,'x'], [20,False,'t'], [30,False,'a'], [40,True,'b']]
3
df=pd.DataFrame(l,index=['M','N','O','P'], columns=['A','B','C'])
4
print(df)
A B C
M 10 True x
N 20 False t
O 30 False a
P 40 True b
import pandas as pd
d= {'A':[10,20,30,40], 'B':[True,False,False,True], 'C':['x','t','a','b']}
df=pd.DataFrame(d,index=['M','N','O','P'])
print(df)
1
import pandas as pd
2
d= {'A':[10,20,30,40], 'B':[True,False,False,True], 'C':['x','t','a','b']}
3
df=pd.DataFrame(d,index=['M','N','O','P'])
4
print(df)
A B C
M 10 True x
N 20 False t
O 30 False a
P 40 True b
1
2
import pandas as pd
3
d1 = {"w": 10, "x": 20, "y": 30, "z": 40}
4
d2 = {"w": 2, "x": 3, "y": 4, "z": 5}
5
d3 = {"w": 9, "x": 8, "y": 6, "z": 4}
6
data = {'A':d1, 'B':d2, 'C':d3}
7
df = pd.DataFrame(data)
8
print(df)
A B C
w 10 2 9
x 20 3 8
y 30 4 6
z 40 5 4
1
import pandas as pd
2
d = {'W':}
*huehuehue
import pandas as pd
d = {'Gender':['F','M','F','M','M'], 'Height':['189cm', '156cm', '167cm', '171cm', '169cm'], 'Weight':['89kg', '62kg', '67kg', '76kg', '73kg']}
df = pd.DataFrame(d, index = ['Marshall', 'Lily', 'Robin', 'Barney', 'Ted'])
print(df)
print("______")
print(df.loc['Barney':, 'Height':])
print("_____")
print(df.loc['Marshall':, 'Weight':])
print("____")
print(df.loc['Lily':'Lily', 'Gender':])
print("____")
print(df.loc['Robin':, 'Gender':'Gender'])
print("_______")
print(df.loc['Marshall':'Robin', 'Gender':'Height'])
Gender Height Weight
Marshall F 189cm 89kg
Lily M 156cm 62kg
Robin F 167cm 67kg
Barney M 171cm 76kg
Ted M 169cm 73kg
Barney 171cm 76kg
Ted 169cm 73kg
Marshall 89kg
Lily 62kg
Robin 67kg
Barney 76kg
Ted 73kg
Lily M 156cm 62kg
Robin F
Barney M
Ted M
Marshall F 189cm
Lily M 156cm
Robin F 167cm
import pandas as pd
d = {'1st Period':['ACC','ENG','ACC','BST','BST'],'2nd Period':['ECO','ACC','ECO','ACC','ACC'], '3rd Period':['ENG','IP','BST','IP','IP'],'4th Period':['BST','ECO','IP','ECO','ECO']}
df = pd.DataFrame(d, index = ['Sat','Mon','Tue','Wed','Thur'])
print(df)
print("_______")
print(df.loc['Wed':'Wed','1st Period':])
print("_______")
print(df.loc['Thur':,'3rd Period':])
print("_______")
print(df.loc[:'Sat',:'4th Period'])
print("_______")
print(df.loc['Tue':'Tue','1st Period':'1st Period'])
print("________")
print(df.loc['Sat':, '4th Period':])
1st Period 2nd Period 3rd Period 4th Period
Sat ACC ECO ENG BST
Mon ENG ACC IP ECO
Tue ACC ECO BST IP
Wed BST ACC IP ECO
Thur BST ACC IP ECO
Wed BST ACC IP ECO
Thur IP ECO
Sat ACC ECO ENG BST
Tue ACC
Sat BST
Mon ECO
Tue IP
Wed ECO
Thur ECO