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

Chapter 1 Python Pandas - I Type C Long Answer

The document contains 16 code snippets demonstrating the use of pandas to create and manipulate Series and DataFrame objects. It shows how to import pandas, create Series from lists of data, add indexes, perform calculations on Series, create DataFrames from dictionaries of data, and extract values from DataFrames.
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)
413 views

Chapter 1 Python Pandas - I Type C Long Answer

The document contains 16 code snippets demonstrating the use of pandas to create and manipulate Series and DataFrame objects. It shows how to import pandas, create Series from lists of data, add indexes, perform calculations on Series, create DataFrames from dictionaries of data, and extract values from DataFrames.
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

1.

import pandas as pd
data = [20,30,25,35,40,39,28]
Temp1 = pd.Series(data)
print(Temp1)

2. import pandas as pd
data = [20, 30, 25, 35, 40, 39, 28]

Temp1 = pd.Series(data,index=['monday','tuesday','\
Wednesday','Thursday','Friday','Saturday','Sunday '])

print(Temp1)

3. import pandas as pd
avg_tem = [28, 30, 25, 35, 37, 39, 28, 28, 30, 25, 35, 37, 39, 28, 28, 30, 25, 35, 37, 39, 28, 28,
30, 25, 35, 37, 39, 28, 28, 30, 25]
Temp1 = pd.Series(avg_tem)

print(f'first 7 days \n {Temp1.head(7)} ')


print(f'last 7 days\n {Temp1.tail(7)} ')

4.# (a)

import pandas as pd

avg_tem = [28.4,30.55,25,35,37,39,28,\
28,30.8,25.0,35,35.4,29,27.1,\
24,30,21,31,37,39,32.5,\
28,30,28.8,35,37,39,28,\
28,30,25]

Temp1 = pd.Series(avg_tem[:7])
Temp2 = pd.Series(avg_tem[8:15])
Temp3 = pd.Series(avg_tem[16:23])
Temp4 = pd.Series(avg_tem[23:30])

print('Average temp of week1 \n', Temp1.sum()/7)


print('Average temp of week2 \n', Temp2.sum()/7)
print('Average temp of week3 \n', Temp3.sum()/7)
print('Average temp of week4 \n', Temp4.sum()/7)
#(b)

import pandas as pd

avg_tem = [28.4,30.55,25,35,37,39,28,\
28,30.8,25.0,35,35.4,29,27.1,\
24,30,21,31,37,39,32.5,\
28,30,28.8,35,37,39,28,\
28,30,25]

Temp1 = pd.Series(avg_tem[:7])
Temp2 = pd.Series(avg_tem[8:15])
Temp3 = pd.Series(avg_tem[16:23])
Temp4 = pd.Series(avg_tem[23:30])

sum = Temp1.sum() + Temp2.sum() + Temp3.sum() + Temp4.sum()


print('Average temp of entire month \n', sum/len(avg_tem))

5. Case study run it.

6. import pandas as pd
indx=["item1", "item2", "item3", "item4", "item5"]

s1 = pd.Series( [ 160, 75, 89, 75, 85 ] , index=indx )


s2 = pd.Series( [ 86, 89, 70, 85, 90 ] , index=indx )
s3 = pd.Series( [ 85, 75, 60, 75, 72 ] , index=indx )
s4 = pd.Series( [ 372, 92, 85, 107, 85 ] , index=indx )
s5 = pd.Series( [ 60, 75, 90, 75, 77 ] , index=indx )
s6 = pd.Series( [ 60, 85, 45, 60, 85 ], index=indx )
s7 = pd.Series( [ 286, 75, 66, 75, 86 ], index=indx )
s8 = pd.Series( [ 60, 72, 200, 70, 75 ], index=indx )
s9 = pd.Series( [ 86, 75, 60, 85, 70 ], index=indx )
s10 = pd.Series( [ 60, 89, 90, 75, 85 ] , index=indx )
s11 = pd.Series( [ 70, 75, 78, 86, 55 ] , index=indx )
s12 = pd.Series( [ 86, 85, 85, 75, 53 ] , index=indx )

yearlySal = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12


print( 'Yearly Sales item-' ,yearlySal )

print('Maximum sales of item made' , end='> ' )


itemlist = list ( yearlySal )

print( indx [ itemlist.index ( max ( itemlist ) ) ] )


print()
List = [ s1 , s2 , s3 , s4 , s5 , s6 , s7 , s8 , s9 , s10 , s11 , s12 ] #list of all series
lst=[]

print( "Maximum sales for individual items" )


print()

dic={ 1:s1 , 2:s2 , 3:s3 , 4:s4 , 5:s5 , 6:s6 , 7:s7 , 8:s8 , 9:s9 , 10:s10 , 11:s11 , 12:s12 }
max = 0
for i in range( 5 ) :
print ( f'Maximum sales of {indx[i]} made: ' , end=' ' )
max = 0
for j in dic:
if max < list ( dic [ j ] ) [ i ] :
max = list( dic[ j ])[ i ]
s=j
print(s)

7. import pandas as pd

Term1 = pd.Series([40,35,25,20,22,26,28,29,23,28])
Term2 = pd.Series([28,15,37,38,45,41,48,47,30,20])
Term3 = pd.Series([36,23,34,31,21,22,23,24,26,28])
final_mark = (Term1*25)/100 + (Term2*25)/100 + (Term3*50)/100

print(final_mark)

8. import pandas as pd
s = pd.Series([40,35,25,20,22,26,28,29,23,28])

print("shape of Series :", s.shape)


print("dtype of Series :", s.dtype)
print("index of Series :", s.index)
print("nbytes of Series :", s.nbytes)
print("Ndimension of Series :", s.ndim)
print("size of Series :", s.size)
print("NaN values of Series :", s.hasnans)

9. import pandas as pd

d = {"col1":[1,4,3],"\
col2":[6,7,8],'\
col3':[9,0,1]}
df = pd.DataFrame(d)
s = pd.Series(data=df[df.columns[0]])
s1 = pd.Series(data=df[df.columns[1]])
s2 = pd.Series(data=df[df.columns[2]])

10. import pandas as pd

d={"col1":[1,4,3],"\
col2":[6,7,8],'\
col3':[9,0,1]}

df = pd.DataFrame(d)
s = pd.Series(data=df.iloc[0])
s1 = pd.Series(data=df.iloc[1])
s2 = pd.Series(data=df.iloc[2])

11. import pandas as pd


import numpy as np

arr = np.array(list('abcdef'))
print(arr)
s1 = pd.Series(arr)

12. import pandas as pd


import numpy as np

tab = np.arange(5,55,5)
ser = pd.Series(tab)
print(ser)

13. import pandas as pd


import numpy as np

arr = np.array(list('abcdef'))
print(arr)
s1 = pd.Series(arr)

tab = np.arange(5,55,5)
ser = pd.Series(tab)
print(ser)

dataf = pd.DataFrame({"col":ser,"col1":tab})
print(dataf)
14. import pandas as pd
import numpy as np
d = {"name":['name1','name2','name30,','name4','name5'],"\
zone":['zone1','zone2','zone3','zone4','zone5'],"sales":[100,200,150,350,250]}

df = pd.DataFrame(d)
print(df)

15. import pandas as pd


import numpy as np

d = {"name":'name',"empno":1}
d1 = {"name":'name1',"empno":2}
d2 = {"name":'name2',"empno":3}
d3 = {"name":'name3',"empno":4}
s = pd.DataFrame([d,d1,d2,d3])
print(s)

16. import pandas as pd


import numpy as np

list = [{"old price":[10,20,30,50,40]},{"new price":[15,20,25,36,50]},{"change":[5,0,-5,-14,10]}]


df = pd.DataFrame(list[0])
df["new price"] = list[1]['new price']
df["change"] = list[2]["change"]
print(df)

You might also like