Select any row from Dataframe in Pandas | python

Python Methods and Functions

# import pandas as pd

import pandas as pd 

 
# Create data frame

df = pd.DataFrame ({ `Date` : [ ` 10/2 / 2011` , `11/2 / 2011` , ` 12/2 / 2011` , `13/2 / 11` ], 

`Event` : [ `Music` , ` Poetry` , `Theater` , ` Comedy ` ], 

  ` Cost` : [ 10000 , 5000 , 15000 , 2000 ]}) 

 

 
# using the interrors () method

 
# Create empty list

Row_list = [] 

 
# Iterate over each line

 

for index, rows in df.iterrows (): 

  

  # Create a list for the current line

my_list = [rows.Date, rows.Event, rows.Cost] 

 

# add list to final list

Row_list.append (my_list) 

 
# Print list

print (Row_list) 

Exit:

 [[`1 0/2 / 2011`, `Music`, 10000], [` 11/2 / 2011`, `Poetry`, 5000], [` 12/2 / 2011`, `Theater`, 15000], [` 13 / 2 / 11`, `Comedy`, 2000]] 

 
# Print the first 2 elements

print (Row_list [: 2 ]) 

Exit:

 [[`10/2 / 2011`,` Music`, 10000], [`11/2 / 2011`,` Poetry`, 5000 ]] 




Get Solution for free from DataCamp guru