Python Forum
Pandas Indexing with duplicates
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas Indexing with duplicates
#1
Hello guys,first time on this forum.

I am working in geomatic processes automation and I have a little question regarding pandas.
I am currently working with a pd.dataframe indexed by street names. The same street name is present several times in my dataframe index.
I am looking for a way to get values based on the index (ex:'WashingtonStreet'). I would like the pandas to return all the rows indexed
with this particular string. Right now, all I am able to get is the first encounter of that index as a returned row :


print(sub_df.loc['RangSaint_Joseph'])

My dataframe structure looks like this :
Index : Street names
FID
X coordinate
Y coordinate
Bearing
Street names

Thanks, and if you have comments on how to improve my thread, feel free.

M.C
Reply
#2
Try this:

df.ix[['Your street name goes here']]
or
df.ix[['Street1', 'Street2',]]
Reply
#3
Hi @scidam,

your function : df.ix[['Street name']] work but only returns the first occurence with the name of the street. As I indicated, my index is the street names and contains many duplicates. For instance, 'WashingtonStreet' would occur 30 times in my database since there are 30 points on that street. I would like to be able to print them all with 1 command.

my data structure would look like this :
Output:
Index FID X Y Bearing E_ID WashingtonStreet 55 6156789 51234456 279.5 '' WashingtonStreet 56 6156413 51234446 279.5 '' WashingtonStreet 77 6156695 51234785 279.5 '' WashingtonStreet 98 6156744 51234748 279.5 '' WashingtonStreet 52 6156784 51234963 279.5 ''

I found my answer. By moving the index (street names) back as a column, the following syntax worked :

print(sub_df[sub_df.AQROUTES_3=='AvenueMermoz'])
Reply
#4
Look at the following example

import pandas as pd
df = pd.DataFrame({'streets':['street1'] * 3+['street2'] * 4, 'unit': range(7)}).set_index('streets')
I can select all streets by name, e.g. (street1):

df.ix[['street1']]
Output:
unit streets street1 0 street1 1 street1 2
Your solution is good as well.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add group number for duplicates atomxkai 2 1,094 Dec-08-2022, 06:08 AM
Last Post: atomxkai
  Counting Duplicates in large Data Set jmair 3 1,091 Dec-07-2022, 09:42 AM
Last Post: paul18fr
  Convert indexing For Loop from MATLAB (uses numpy and pandas) bentaz 3 4,136 Mar-20-2018, 08:29 PM
Last Post: bentaz
  jupyter pandas remove duplicates help okl 3 7,470 Feb-25-2018, 01:11 PM
Last Post: glidecode

Forum Jump:

User Panel Messages

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