Pandas DataFrame loc

Column name based position location using loc
We used DataFrame of 35 rows by reading from a CSV file, you can download and check our test.csv file here.
import pandas as pd 
my_data=pd.read_csv('test.csv')
print(my_data)
Output
    id         name  class  mark     sex
0    1     John Deo   Four    75  female
1    2     Max Ruin  Three    85    male
2    3       Arnold  Three    55    male
3    4   Krish Star   Four    60  female
 ----------------
 ----------------
 ---------------
You can display all 35 rows , Now let us try our scripts.
This line will display one column class of all rows
my_data.loc[:,'class'] # all rows of column class
Output
0      Four
1     Three
2     Three
-------
-------
All 35 rows will be displayed in above code.
All rows showing with two columns class, name
my_data.loc[:,['class','name']] # all rows of column class and name
	class	name
0	Four	John Deo
1	Three	Max Ruin
2	Three	Arnold
3	Four	Krish Star
------------
------------
------------
All 35 rows will be displayed in above code

Value at position

First rows with two columns
my_data.loc[0,['class','name']] # first row with two columns class and name
Output
class        Four
name     John Deo
Name: 0, dtype: object

2nd and 3rd rows with two columns

my_data.loc[[2,3],['class','name']] # 2nd and 3rd row with two columns class and name
Output
	class	name
2	Three	Arnold
3	Four	Krish Star

2nd and 3rd rows with all columns

my_data.loc[[2,3],:] # 2nd and 3rd row with two columns class and name
Output
	id	name	class	mark	sex
2	3	Arnold	Three	55	male
3	4	Krish Star	Four	60	female

Matching data

While reading csv file we will add index to name column and then search.
import pandas as pd 
my_data=pd.read_csv('test.csv',index_col='name')
my_data.loc['Tade Row']
Output
id         15
class    Four
mark       88
sex      male
Name: Tade Row, dtype: object
Matching two names
my_data.loc[['Tade Row','John Deo']] # printing row with two name matching
Output
		id	class	mark	sex
name				
Tade Row	15	Four	88	male
John Deo	1	Four	75	female

All matching records

import pandas as pd 
my_data=pd.read_csv('test.csv',index_col='class')
my_data.loc['Four'] # printing all rows with class='Four'
Output
	id	name		mark	sex
class				
Four	1	John Deo	75	female
Four	4	Krish Star	60	female
Four	5	John Mike	60	female
Four	6	Alex John	55	male
Four	10	Big John	55	female
Four	15	Tade Row	88	male
Four	16	Gimmy	88	male
Four	21	Babby John	69	female
Four	31	Marry Toeey	88	male

Rows between ranges

All the records between two id
import pandas as pd 
my_data=pd.read_csv('test.csv',index_col='id')
my_data.loc[4:6] # printing rows between two ids 
Output
	name		class	mark	sex
id				
4	Krish Star	Four	60	female
5	John Mike	Four	60	female
6	Alex John	Four	55	male
In above code we have two unique ids and rows between these two ids will be displayed. If data are not unique then there will be an error. Try be using two class names, you will get error "Cannot get left slice bound for non-unique label: 'Five'"

Between two names ( column data)

import pandas as pd 
my_data=pd.read_csv('test.csv',index_col='name')
#print(my_data)
my_data.loc['Ronald':'Bigy']  # Records between two names 
Here are records between name 'Ronald' and 'Bigy'
	id	class	mark	sex
name				
Ronald	11	Six	89	female
Recky	12	Six	94	female
Kty	13	Seven	88	female
Bigy	14	Seven	88	female

Record of matching string

import pandas as pd 
my_data=pd.read_csv('test.csv')
print(my_data.loc[my_data['name']=='Alex John']) # Search for name
Output

   id       name class  mark   sex
5   6  Alex John  Four    55  male

endswith : string ending with

print(my_data.loc[my_data['name'].str.endswith('John')])
Output
    id        name class  mark     sex
5    6   Alex John  Four    55    male
9   10    Big John  Four    55  female
20  21  Babby John  Four    69  female

Getting rows from end

Last record
import pandas as pd 
my_data=pd.read_csv('test.csv')
print(my_data.tail(1)) # Last  records
Output
    id        name class  mark     sex
34  35  Rows Noump   Six    88  female
Last n records ( five records from end )
print(my_data.tail(5)) # Last five records 
Output
    id         name  class  mark     sex
30  31  Marry Toeey   Four    88    male
31  32    Binn Rott  Seven    90  female
32  33    Kenn Rein    Six    96  female
33  34     Gain Toe  Seven    69    male
34  35   Rows Noump    Six    88  female
Pandas DataFrame iloc - rows and columns by integers
Your Rating




Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer