Python Forum
Problem with If statement and dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with If statement and dataframe
#1
I am trying to use an If statement with a Dataframe. Without the If statement the Dataframe prints out beautifully, But it prints out all 63 rows. I am trying to check if field in Dataframe matched user input. Can someone tell me what I am doing wrong?

def load_track_to_Handi():
	my_tracklabel= Label(root, text ="                                                                               ", fg= "#F50808", font = ("sans_serif" , 16)).place(x=380, y=430)
	read_csvfile_into_array()
	
		
	global track_abrev
	global race_number
	global xx
	
	#if fileFound ==True:

		
		 
	
	
	hcount = 0
	
	with open( 'C://guis/f_racefiles/'+ track_abrev + '.csv' , 'r') as race_file:

		
		df = pd.read_csv(race_file, header =None)
		
		xx = df.iloc[ :,0:19 ].values
		
		

		for i in range(0,len(xx)):
			if (xx[hcount][1] == race_number):
				print(xx)[hcount][18])					
				hcount+=1	                          				       		
I remove if statement and prints out with no problem. Race number is an integer and I believe the field in the dataframe is an interger also, as it is a race number.
Reply
#2
This is the printout I get when I remove the if statement. I am trying to match user input (racenumber0 with the 2nd column in the dataframe, with the code in above post.

[['AP ' 1 'D' ... 42 3.0 'EYE ON THE FINISH']
['AP ' 1 'D' ... 46 10.0 'GODSGIFT']
['AP ' 1 'D' ... 82 8.0 'LILLET']
...
['AP ' 8 'D' ... 35 6.0 'WILDCAT LITE']
['AP ' 8 'D' ... 46 15.0 'GREELEYS ICE']
['AP ' 8 'D' ... 29 12.0 'JIVE DADDY']]

This was the code I changed and it worked. I am amazed...LOl.
for i in range(0,len(xx)):
			if int(xx[hcount][1])== int(race_number):
			 
				print(xx[hcount][18])					
				hcount+=1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  2-dataframe, datetime lookup problem Mark17 0 1,215 Jan-27-2022, 01:02 AM
Last Post: Mark17
  problem with while statement. BobSmoss 3 1,623 Jan-08-2022, 03:22 PM
Last Post: BobSmoss
  problem writing dataframe to oracle aliyesami 4 2,586 Sep-25-2021, 11:20 PM
Last Post: SamHobbs
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 4,164 Aug-29-2021, 12:39 PM
Last Post: snippsat
  Problem in if-else statement shantanu97 2 2,389 Apr-09-2021, 06:37 AM
Last Post: shantanu97
  Dataframe problem stylingpat 3 2,764 Mar-22-2021, 10:36 AM
Last Post: stylingpat
  multiple condition if statement problem FelixReiter 3 2,542 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  optimization problem for dataframe manipulation fimmu 0 1,438 Aug-31-2020, 06:02 PM
Last Post: fimmu
  Problem with If else statement Milfredo 5 2,538 Aug-30-2020, 06:32 PM
Last Post: Milfredo
  Dataframe mean calculation problem: do we have to loop? sparkt 1 2,135 Aug-28-2020, 02:41 PM
Last Post: sparkt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020