100% found this document useful (1 vote)
59 views

Pandas Questions

1. The document discusses various methods to access data from Pandas DataFrames including single columns, multiple columns, single rows, multiple rows, and individual values. 2. It also covers modifying DataFrames by adding or deleting rows and columns, renaming indexes and columns, and filtering DataFrames. 3. The document provides examples of code snippets using DataFrame methods like .loc, .iloc, .drop(), .rename(), and boolean filtering to select or modify portions of a DataFrame.

Uploaded by

Karan Singh
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
100% found this document useful (1 vote)
59 views

Pandas Questions

1. The document discusses various methods to access data from Pandas DataFrames including single columns, multiple columns, single rows, multiple rows, and individual values. 2. It also covers modifying DataFrames by adding or deleting rows and columns, renaming indexes and columns, and filtering DataFrames. 3. The document provides examples of code snippets using DataFrame methods like .loc, .iloc, .drop(), .rename(), and boolean filtering to select or modify portions of a DataFrame.

Uploaded by

Karan Singh
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/ 7

To Access data of single /multiple columns

Dataframe :df
Syntax: df[colname], or df.colname(single
column)
Syntax: df[[col1,col2,col3]] (multiple col)
NAME MARKS SPORTS
0 AJAY 90 CRICKET
1 VIJAY 80 FOOTBALL
2 DEV 100 BADMINTON
3 RAM 95 KABADDI
4 DEEP 92 ATHLETICS
1. Write a code to display data of sports column
Ans
2. Write a code to show data of name column
Ans
3. Write a code to display data of all columns
Ans:
4. Write a code to access data of marks and sports colums
Ans:
5. To access data of marks column
6. To show data of name and sports column
To Access data of single /multiple rows
Using loc[] /iloc[]
Syntax: dfob.loc[startrow : endrow :gap , startrcol : endcol:gap]
DATAFRAME :df1

ANO NAME RNO FEES


I 201 RAJ 1 200
II 202 RAM 2 400
III 203 BAHUBALI 3 500
IV 204 AJAY 4 600
V 205 PIYUSH 5 300
VI 206 VINIT 6 700
To access data of single row
1. To access /display data of row 2nd
df1.loc[‘II’] / df1.iloc[1] / df1.loc[‘II’,:] / df1.iloc[1,:]
2. To access/display data of rows from 2nd to 5th
Ans: df1.loc['II':'V'] / df1.loc['II':'V',:] / df1.iloc[1:5]
/ df1.iloc[1:5,:]
3. To access/ display data of row 1st ,3rd ,5th so on
Ans: df1.loc['I':'VI':2] / df1.loc['I':'VI':2,:] / df1.loc[::2]
/ df1.loc[::2, : ] / df1.iloc[0:6;2] / df1.iloc[0:6;2,:] /
df1.iloc[::2]
Q1. To access display 3rd row
Q2. To access data of 2nd and 4th , 6th row
Q3. To access data of row 1st to 3rd
Q4 To access row 5th
Q5. To access row 1st , 4th,
Q6. To access row from 2nd to 6th
TO ACCESS COLUMN ONLY
SINGLE COLUMN
df1[‘ANO’] / df1.ANO / df1.loc[:,'ANO'] / df1.iloc[:,0]
MULTIPLE COLUMN
ANS : df1[['ANO','NAME', 'FEES']]
df1.loc[:,'ANO':'FEES'] / df1.loc[:,:] / df1.iloc[:,0:4] / df1.iloc[:,:]
To access both row and columns
1. To access 3rd row and col ano to fees /(Ist to 4th )

Ans: df1.loc['III','ANO':'FEES'] / df1.loc['III', : ] /


df1.iloc[2,0:3] / df1.iloc[2, : ]
2. To display 2nd ,4th, and 6th row and Ist ,3rd col

Ans: df1.loc['II':'VI':2,'ANO':'FEES':2] / df1.loc['II': :2,'ANO': :2]

Q1 To display Ist row and Ist col


Ans:
Q2. To display rows 3rd onward and cols 2nd onward
ANS:
Q3 To display Ist , 4th row and 2nd and 4th col
Q4. To access all rows and cols
TO SELECTING/ACCESSING INDIVISUAL VALUE

Syntax: df.colname[index]
df1.NAME['II'] / df1.NAME[1] /

MODIFY /ASSINGING VALUES/add new row/columns


Syntax: df[COLLABEL]= new value
To assign values
1. To change fees by 5000
Ans: df1['FEES']=5000
2. To insert different fees
Ans: df1['FEES']=[5000,6000,6000,7000,8000,5000]
Or
df1.loc[:,'FEES']=[500,600,700,800,500,400]
Q1 To display whose name is Bahubali
Q2. To display whose admno is 206
Q3. To change data of column rno by values[ a,b,c,d,e]
Q4. To change admno by value 500

To add new row

df1.loc['ix',:]=[204,'mohak',9,600]
or
df1.at['x',:]=[204,'mohak',9,600]
or
df1.loc[6]=[207,'don',7,600]
Q1. To add new row with index xi
Q2 To add record in dataframe df1 (xii)

To add new col

df1['cno']=[1,2,3,4,5,6,7,8,9,10]
or
df1.loc[:,'section']=['A','B','C','D','E','F','G','H','I','J']
or
df1.at[:,'ADDRESS']=['UP','MP','AP','HP','DEL','MUM']

Q1. To add new col fname


Q2. To add new col class

Q1. TO DELETE COLUMNS

Ans: del df1['FEES'] / df1.drop([‘FEES’],axis=1)


To DELETE MULITPLE COLUMNS
df1.drop([0,1,2,3],axis=1)

Q1. To delete column name


Q2 Delete column fname
Q3 delete column contactno
Q4 To delete column ano
To delete columns of dataframe in slice format

df1.drop(df1.columns[0:3],axis=1)

TO DELETE ROWS

df1.drop(['V'])

To delete multiple rows


df1.drop([0,1,2,3],axis=1)

Q1. To delete vith row


Ans: df1.drop([‘vi’])
Q2. To delete 2nd row
Ans: df1.drop([‘II’])
To delete rows of dataframe in slice format
df1.drop(df1.index[0:3])

To Delete series
del seriesobj
eg del s

To Delete Dataframe
del dfobj
eg del df

To Transpose Dataframe
Dfobj.T
Eg df.T

TO RENAME THE ROWS


df1.rename(index={'I':'A','II':'B','III':'C','IV':'D','V':'E','VI':'F'})
or
df1.index=['a','b','c','d','e','f']

TO RENAME COLUMNS
df1.rename(columns={'ANO':'RNO','NAME':'STDNAME','RNO':'AN
O'})
or
df1.columns=['a','b','c','d']

TO RENAME Series
New series obj= seriesobj.rename(‘newname’)
s1=s.rename('abc')
To check name of series
Seriesobj.name
FILETERING

Q1. To display fees whose fees is more than 500


ANS: df1[df1['FEES']>500]
Q2. To display fees more than equals to 400 and less than equals
to 600
Ans: df1[(df1.FEES>=400) & (df1.FEES<=600)]
Or
df1[(df1['FEES']>=400) & (df1['FEES']<=600)]
Q3. To display details whose name is vinit
ANS: df1[df1['NAME']=='VINIT']
Q4 To details either whose name is vinit or rno is more than four.
ANS: df1[(df1['NAME']=='VINIT') | (df1['RNO']>4)]

head()/ tail()
Q1. There are 3500 rows in dataframe to

You might also like