Pandas Indexing with duplicates - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Pandas Indexing with duplicates (/thread-16580.html) |
Pandas Indexing with duplicates - energerecontractuel - Mar-05-2019 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 RE: Pandas Indexing with duplicates - scidam - Mar-05-2019 Try this: df.ix[['Your street name goes here']]or df.ix[['Street1', 'Street2',]] RE: Pandas Indexing with duplicates - energerecontractuel - Mar-06-2019 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 :
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'])
RE: Pandas Indexing with duplicates - scidam - Mar-07-2019 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']] Your solution is good as well.
|