2

I have a pandas series that looks like this, extracted on querying a dataframe.

t_loc= 312 False 231 True 324 True 286 False 123 False 340 True 

I want only the indices that have 'True' boolean value.

I tried t_loc.index gives me all the indices. t_loc['True'] or t_loc[True] are both futile. Need help. Also, I need to update these locations with a single number if True. How can I update a column in a dataframe given the location numbers ?

Desired O/P: [231,324,340]

Need to update eg. df[col1] @ 231.. is it df[col1].loc[231] ? How to specify multiple locations? Can we pass the entire list since I need to update it with only one value for all the locations ?

4
  • t_loc.index[t_loc] Commented Aug 1, 2019 at 20:56
  • Great !! Worked.. Thanks.. For the column update can I use df['col1'].loc[a] = 40 (The value I want to update) given a= t_loc.index[t_loc] Commented Aug 1, 2019 at 21:06
  • Everything worked.. !! Commented Aug 1, 2019 at 21:24
  • Actually, you can simply do df.loc[t_loc, 'col1'] = 40. Commented Aug 1, 2019 at 21:25

2 Answers 2

1

This actually works too :

t_loc.index[t_loc == True/False] 
Sign up to request clarification or add additional context in comments.

Comments

0

u can try this as well

t_loc.astype(int).index[t_loc == 1] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.