Maxime Campeau
Maxime Campeau

Reputation: 109

Pandas - Iterating over an index in a loop

I have a weird interaction that I would need help with. Basically :

1) I have created a pandas dataframe that containts 1179 rows x 6 columns. One column is street names and the same value will have several duplicates (because each line represents a point, and each point is associated with a street).

2) I also have a list of all the streets in this panda dataframe.

3)If I run this line, I get an output of all the rows matching that street name:

print(sub_df[sub_df.AQROUTES_3=='AvenueMermoz'])

Result :

       FID    AQROUTES_3 ...      BEARING E_ID
983    983  AvenueMermoz ...   288.058014     
984    984  AvenueMermoz ...   288.058014     
992    992  AvenueMermoz ...   288.058014     
1005  1005  AvenueMermoz ...   288.058014     
1038  1038  AvenueMermoz ...   288.058014     
1019  1019  AvenueMermoz ...   288.058014 

However, if I run this command in a loop with the string of my list as the street name, it returns an empty dataframe :

x=()
for names in pd_streetlist:
    print(names)
    x=names
    print(sub_df[sub_df.AQROUTES_3 =="'"+str(x)+"'"])
    x=()

Returns :

RangSaint_Joseph
Empty DataFrame
Columns: [FID, AQROUTES_3, X, Y, BEARING, E_ID]
Index: []
AvenueAugustin
Empty DataFrame
Columns: [FID, AQROUTES_3, X, Y, BEARING, E_ID]
Index: []

and so on...

I can't figure out why. Anybody has an idea?

Thanks

Upvotes: 2

Views: 76

Answers (2)

sophros
sophros

Reputation: 16660

I believe the issue is in this line:

print(sub_df[sub_df.AQROUTES_3 =="'"+str(x)+"'"])

To each names you add unnecessarily quote characters at the beginning and at the end so that each valid name of the street (in your example 'AvenueMermoz' turns into "'AvenueMermoz'" where we had to use double quotes to enclose single-quoted string).

As @busybear has commented - there is no need to cast to str either. So, the corrected line would be:

print(sub_df[sub_df.AQROUTES_3 == x])

Upvotes: 1

Rodney Melchers
Rodney Melchers

Reputation: 16

So youre adding quotation marks to the filter which you shouldnt. now youre filtering on 'AvenueMermoz' while you just want to filter on AvenueMermoz .

so

print(sub_df[sub_df.AQROUTES_3 =="'"+str(x)+"'"])

should become

print(sub_df[sub_df.AQROUTES_3 ==str(x)])

Upvotes: 0

Related Questions