Sudarsana Shankar
Sudarsana Shankar

Reputation: 41

Getting only True values and their respective indices from a Pandas series

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 ?

Upvotes: 1

Views: 4631

Answers (2)

Mehdi Fekih
Mehdi Fekih

Reputation: 95

This actually works too :

t_loc.index[t_loc == True/False]

Upvotes: 1

Aakash Raut
Aakash Raut

Reputation: 1

u can try this as well

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

Upvotes: 0

Related Questions