[英]How to get the index value of column value compared with another column value
I want something like this. 我想要这样的东西。
Index Sentence
0 I
1 want
2 like
3 this
Keyword Index
want 1
this 3
I tried with df.index("Keyword")
but its not giving for all the rows. 我尝试了
df.index("Keyword")
但它没有提供所有行。 It will be really helpful if someone solve this. 如果有人解决了这个问题,将非常有帮助。
Use isin
with boolean indexing
only: 仅将
isin
与boolean indexing
使用:
df = df[df['Sentence'].isin(['want', 'this'])]
print (df)
Index Sentence
1 1 want
3 3 this
EDIT: If need compare by another column: 编辑:如果需要比较另一列:
df = df[df['Sentence'].isin(df['Keyword'])]
#another DataFrame df2
#df = df[df['Sentence'].isin(df2['Keyword'])]
And if need index values: 如果需要索引值:
idx = df.index[df['Sentence'].isin(df['Keyword'])]
#alternative
#idx = df[df['Sentence'].isin(df['Keyword'])].index
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.