Python | Индекс панд. Где
Pandas Index - это неизменяемый ndarray, реализующий упорядоченный, нарезанный набор. Это базовый объект, который хранит метки осей для всех объектов pandas.
Pandas Index.where function return an Index of same shape as self and whose corresponding entries are from self where cond is True and otherwise are from other.
Syntax: Index.where(cond, other=None)
Parameter :
cond : boolean array-like with the same length as self
other : scalar, or array-likeReturns : Index
Example #1: Use Index.where function to return an Index, in which we select the value from the other Index if value of this Index is not smaller than 100.
# importing pandas as pdimport pandas as pd # Creating the first indexidx1 = pd.Index([900, 45, 21, 145, 38, 422]) # Creating the second indexidx2 = pd.Index([1100, 1200, 1300, 1400, 1500, 1600]) # Print the first indexprint(idx1) # Print the second indexprint(idx2) |
Выход :


Now we will use Index.where function to return an Index, in which we select the value from the other Index if value of this Index is not smaller than 100.
# return the new index based on the conditionresult = idx1.where(idx1 < 100, idx2) # Print the resultprint(result) |
Output :
As we can see in the output, the Index.where function has successfully returned an Index object satisfying the passed condition.
Example #2 : Use Index.where function to return an Index, which satisfy the passed condition.
# importing pandas as pdimport pandas as pd # Creating the first indexidx1 = pd.Index([900, 45, 21, 145, 38, 422]) # Creating the second indexidx2 = pd.Index([1100, 1200, 1300, 1400, 1500, 1600]) # Print the first indexprint(idx1) # Print the second indexprint(idx2) |
Выход : 

Now we will use Index.where function to return an Index, in which we select the value from the other Index if value of other Index minus 1200 is not smaller than idx1.
# return the new index based on the conditionresult = idx1.where((idx2 - 1200) < idx1, idx2) # Print the resultprint(result) |
Output :
As we can see in the output, the Index.where function has successfully returned an Index object satisfying the passed condition.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.