0% found this document useful (0 votes)
20 views9 pages

9.9.24 Revision

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

9.9.24 Revision

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

>>> import pandas as pd

>>> import numpy as np

>>> df=pd.DataFrame()

>>> df

Empty DataFrame

Columns: []

Index: []

>>> dict1={'a':20,'d':30,'g':40}

>>> df=pd.DataFrame([dict1])

>>> df

a d g

0 20 30 40

>>> df=pd.DataFrame({'dict1':dict1})

>>> df

dict1

a 20

d 30

g 40

>>> dict2={'a':[22,33,44],'b':[44,55,66]}

>>> df=pd.DataFrame(dict2)

>>> df

a b

0 22 44

1 33 55

2 44 66

>>> s1=pd.Series([22,33,44],index=['a','b','c'])

>>> s2=pd.Series([55,66,77],index=['a','b','c'])

>>> s3=pd.Series([66,77,88],index=['a','b','c'])

>>> df=pd.DataFrame([s1,s2,s3])
>>> df2=pd.DataFrame({'s1':s1,'s2':s2.'s3':s3})

SyntaxError: invalid syntax

>>> df2=pd.DataFrame({'s1':s1,'s2':s2,'s3':s3})

>>> df

a b c

0 22 33 44

1 55 66 77

2 66 77 88

>>> df2

s1 s2 s3

a 22 55 66

b 33 66 77

c 44 77 88

>>> a1=np.array([22,33,44])

>>> a2=np.array([44,55,66])

>>> df3=pd.DataFrame([a1,a2])

>>> df4=pd.DataFrame({'a1':a1,'a2':a2})

>>> df3

0 1 2

0 22 33 44

1 44 55 66

>>> df4

a1 a2

0 22 44

1 33 55

2 44 66

>>> df

a b c

0 22 33 44
1 55 66 77

2 66 77 88

>>> df['d']=[23,34,45]

>>> df

a b c d

0 22 33 44 23

1 55 66 77 34

2 66 77 88 45

>>> df.loc[3]=[45,34,23,12]

>>> df

a b c d

0 22 33 44 23

1 55 66 77 34

2 66 77 88 45

3 45 34 23 12

>>> df.drop('a',axis=1,inplace=True)

>>> df

b c d

0 33 44 23

1 66 77 34

2 77 88 45

3 34 23 12

>>> df.drop([2,3])

b c d

0 33 44 23

1 66 77 34

>>> df

b c d

0 33 44 23
1 66 77 34

2 77 88 45

3 34 23 12

>>> df.rename({'c':'centre'},inplace=True,axis=1)

>>> df

b centre d

0 33 44 23

1 66 77 34

2 77 88 45

3 34 23 12

>>> dfnew=df

>>> dfnew

b centre d

0 33 44 23

1 66 77 34

2 77 88 45

3 34 23 12

>>> df.loc[2,'centre']=888

>>> df

b centre d

0 33 44 23

1 66 77 34

2 77 888 45

3 34 23 12

>>> df.loc[1,'d']

34

>>> df['centre']>50

0 False

1 True
2 True

3 False

Name: centre, dtype: bool

>>> df[df['centre']>50]

b centre d

1 66 77 34

2 77 888 45

>>> df

b centre d

0 33 44 23

1 66 77 34

2 77 888 45

3 34 23 12

>>> df.loc[,[False,True,False]]

SyntaxError: invalid syntax

>>> df.loc[:,[False,True,False]]

centre

0 44

1 77

2 888

3 23

>>> df.loc[[True,False,True,False],[True,False,False]]

0 33

2 77

>>> df

b centre d

0 33 44 23

1 66 77 34
2 77 888 45

3 34 23 12

>>> df.loc[1:3,'centre':'d']

centre d

1 77 34

2 888 45

3 23 12

>>> df.loc[:,'d']

0 23

1 34

2 45

3 12

Name: d, dtype: int64

>>> df.loc[0:2]

b centre d

0 33 44 23

1 66 77 34

2 77 888 45

>>> df.loc[0:2,:]

b centre d

0 33 44 23

1 66 77 34

2 77 888 45

>>> df.index

Index([0, 1, 2, 3], dtype='int64')

>>> df.columns

Index(['b', 'centre', 'd'], dtype='object')

>>> df.index=['a','b','c','d']

>>> df
b centre d

a 33 44 23

b 66 77 34

c 77 888 45

d 34 23 12

>>> df.columns=['p','q','r']

>>> df

p q r

a 33 44 23

b 66 77 34

c 77 888 45

d 34 23 12

>>> df.dtypes

p int64

q int64

r int64

dtype: object

>>> df.values

array([[ 33, 44, 23],

[ 66, 77, 34],

[ 77, 888, 45],

[ 34, 23, 12]], dtype=int64)

>>> df.shape

(4, 3)

>>> df.size

12

>>> df.T

a b c d

p 33 66 77 34
q 44 77 888 23

r 23 34 45 12

>>> df.head()

p q r

a 33 44 23

b 66 77 34

c 77 888 45

d 34 23 12

>>> df.head(2)

p q r

a 33 44 23

b 66 77 34

>>> df.tail()

p q r

a 33 44 23

b 66 77 34

c 77 888 45

d 34 23 12

>>> df.tail(2)

p q r

c 77 888 45

d 34 23 12

>>> df=pd.read_csv('C:/Users/LAB1-PC29/Desktop/book1.csv')

>>> df

name eng math ip

0 prabhnoor 20 50 10

1 ridham 30 40 10

2 apeksha 40 30 10

3 hitaishi 50 20 10
>>> df.loc[4]=['manya',45,56,67]

>>> df

name eng math ip

0 prabhnoor 20 50 10

1 ridham 30 40 10

2 apeksha 40 30 10

3 hitaishi 50 20 10

4 manya 45 56 67

>>> df['science']=[34,45,56,67,45]

>>> df

name eng math ip science

0 prabhnoor 20 50 10 34

1 ridham 30 40 10 45

2 apeksha 40 30 10 56

3 hitaishi 50 20 10 67

4 manya 45 56 67 45

>>> df.to_csv('C:/Users/LAB1-PC29/Desktop/book2.csv')

>>>

You might also like