Pandas 如果值在列数据框中,则获取行
Pandas Get rows if value is in column dataframe
我有信息增益数据框和 tf 数据框。数据如下所示:
信息增益
Term IG
0 alqur 0.641328
1 an 0.641328
2 ayatayat 0.641328
3 bagai 0.641328
4 bantai 0.641328
5 besar 0.641328
词频
A B A+B
ahli 1 0 1
alas 1 0 1
alqur 0 1 1
an 0 1 1
ayatayat 0 1 1
... ... ... ...
terus 0 1 1
tuduh 0 1 1
tulis 1 0 1
ulama 1 0 1
upaya 0 1 1
假设table信息增益=IG
和 table tf = TF
我想检查 IG.Term 是否在 TF.index 中,然后获取行值,所以它应该是这样的:
Term A B A+B
0 alqur 0 1 1
1 an 0 1 1
2 ayatayat 0 1 1
3 bagai 1 0 1
4 bantai 1 1 2
5 besar 1 0 1
注意:我不再需要 IG 值了
按 Series.isin
with boolean indexing
过滤并将索引转换为列:
df = TF[TF.index.isin(IG['Term'])].rename_axis('Term').reset_index()
print (df)
Term A B A+B
0 alqur 0 1 1
1 an 0 1 1
2 ayatayat 0 1 1
或将 DataFrame.merge
与默认内连接一起使用:
df = IG[['Term']].merge(TF, left_on='Term', right_index=True)
print (df)
Term A B A+B
0 alqur 0 1 1
1 an 0 1 1
2 ayatayat 0 1 1
您可以像这样使用合并来检查它:
ig = pandas.DataFrame([['alqur', 0.641328], ['an', 0.641328]], columns=['Term', 'IG'])
tf = pandas.DataFrame([['ahli', 1, 0, 1], ['alqur', 0, 1, 1], ['an', 0, 1, 1]], columns=['index', 'A', 'B', 'A+B'])
tf = tf.set_index('index')
rows_count, _columns_count = tf.shape
merged = tf.merge(ig, left_on='index', right_on='Term', how='inner')
merged 包含 ig 中没有丢失的术语。
我有信息增益数据框和 tf 数据框。数据如下所示:
信息增益
Term IG
0 alqur 0.641328
1 an 0.641328
2 ayatayat 0.641328
3 bagai 0.641328
4 bantai 0.641328
5 besar 0.641328
词频
A B A+B
ahli 1 0 1
alas 1 0 1
alqur 0 1 1
an 0 1 1
ayatayat 0 1 1
... ... ... ...
terus 0 1 1
tuduh 0 1 1
tulis 1 0 1
ulama 1 0 1
upaya 0 1 1
假设table信息增益=IG 和 table tf = TF
我想检查 IG.Term 是否在 TF.index 中,然后获取行值,所以它应该是这样的:
Term A B A+B
0 alqur 0 1 1
1 an 0 1 1
2 ayatayat 0 1 1
3 bagai 1 0 1
4 bantai 1 1 2
5 besar 1 0 1
注意:我不再需要 IG 值了
按 Series.isin
with boolean indexing
过滤并将索引转换为列:
df = TF[TF.index.isin(IG['Term'])].rename_axis('Term').reset_index()
print (df)
Term A B A+B
0 alqur 0 1 1
1 an 0 1 1
2 ayatayat 0 1 1
或将 DataFrame.merge
与默认内连接一起使用:
df = IG[['Term']].merge(TF, left_on='Term', right_index=True)
print (df)
Term A B A+B
0 alqur 0 1 1
1 an 0 1 1
2 ayatayat 0 1 1
您可以像这样使用合并来检查它:
ig = pandas.DataFrame([['alqur', 0.641328], ['an', 0.641328]], columns=['Term', 'IG'])
tf = pandas.DataFrame([['ahli', 1, 0, 1], ['alqur', 0, 1, 1], ['an', 0, 1, 1]], columns=['index', 'A', 'B', 'A+B'])
tf = tf.set_index('index')
rows_count, _columns_count = tf.shape
merged = tf.merge(ig, left_on='index', right_on='Term', how='inner')
merged 包含 ig 中没有丢失的术语。