Bottom Page

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
Quote
#2
Try this:

df.ix[['Your street name goes here']]
or
df.ix[['Street1', 'Street2',]]
Quote
#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'])
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Learning indexing with python, pick dates dervast 1 135 Jul-11-2019, 07:29 AM
Last Post: scidam
  future warning when using numpy fancy indexing bluefrog 0 743 Nov-20-2018, 09:44 AM
Last Post: bluefrog
  DataFrame.astype('category') duplicates column garikhgh0 3 781 Apr-18-2018, 11:35 PM
Last Post: scidam
  Convert indexing For Loop from MATLAB (uses numpy and pandas) bentaz 3 913 Mar-20-2018, 08:29 PM
Last Post: bentaz
  jupyter pandas remove duplicates help okl 3 1,643 Feb-25-2018, 01:11 PM
Last Post: glidecode
  python array/cell/indexing python 1 768 Feb-08-2018, 11:18 AM
Last Post: DeaD_EyE
  pandas/index.pyx in pandas.index.IndexEngine.get_loc - KeyError jacobs.smith 2 3,404 Nov-17-2016, 04:07 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)