How can I iterate over specific index in pandas?

Question:

So I have a list of index close_index I want to iterate over these specific index and update the column values.

hhinc8,owncar,longitude,latitude,category
5,1,-82.99194508,40.04649963,LRLC
6,2,-82.99584706,40.03738548,LRHC
5,1,-82.99697268,40.02674247,LRLC
6,2,-82.99160441,40.03612997,LRHC
1,2,-82.994716,40.04691778,ERHC
2,1,-82.99793728,40.04385713,ERLC
3,2,-82.98986012,40.03789279,ERHC
.
.
.

close_index =[1,3,4]

now in my code I update the all the close_index’s hhinc8 like
df.loc[close_index,'hhinc8']=2
now I want to update the category of all the close_index by checking the owncar variable. If owncar is 1 category should be LRLC else it should be LRHC.

I thought the best way to do this is by looping over all the index in close_index and checking the owncar and updating the category.

But I am unable to figure out a way to loop over specific index.
As the dataset I have is very big, looping over the whole dataset is very inefficient.
If there is a better way to do this, Please let me know.

Asked By: Tallion 22

||

Answers:

here is one way to do it

# create a dictionary to map owncar to the category value
d={1: 'LRLC', 2: 'LRHC'}


close_index =[1,3,4]

df['category'] = df.loc[close_index]['owncar'].map(d)
df

or simply

hhinc8  owncar  longitude   latitude    category
0   5   1   -82.991945  40.046500   LRLC
1   6   2   -82.995847  40.037385   LRHC
2   5   1   -82.996973  40.026742   LRLC
3   6   2   -82.991604  40.036130   LRHC
4   1   2   -82.994716  40.046918   LRHC
5   2   1   -82.997937  40.043857   LRLC
6   3   2   -82.989860  40.037893   LRHC

to list the rows of close_index

df.loc[df.index.isin(close_index)]
    hhinc8  owncar  longitude   latitude    category
1        6       2  -82.995847  40.037385   LRHC
3        6       2  -82.991604  40.036130   LRHC
4        1       2  -82.994716  40.046918   ERHC
for i in close_index:
    print(df.iloc[df.index == i])
   hhinc8  owncar  longitude   latitude category
1       6       2 -82.995847  40.037385     LRHC
   hhinc8  owncar  longitude  latitude category
3       6       2 -82.991604  40.03613     LRHC
   hhinc8  owncar  longitude   latitude category
4       1       2 -82.994716  40.046918     ERHC
Answered By: Naveed

You can do the following, I didn’t test though,

df[df["owncar"]==1].loc[close_index,'category']=LRLC
df[df["owncar"]!=1].loc[close_index,'category']=LRHC
Answered By: Priya
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.