简体   繁体   English

Pandas 如果值在列 dataframe 中,则获取行

[英]Pandas Get rows if value is in column dataframe

I have Information Gain dataframe and tf dataframe.我有信息增益 dataframe 和 tf dataframe。 the data looks like this:数据如下所示:

Information Gain信息增益

    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

Term Frequency词频

            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

let's say table Information Gain = IG and table tf = TF假设表信息增益 = IG 和表 tf = TF

I wanted to check if IG.Term is in TF.index then get the row values so it should be like this:我想检查 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

NB: I don't need the IG value anymore注意:我不再需要 IG 值了

Filter by Series.isin with boolean indexing and convert index to column:使用boolean indexingSeries.isin过滤并将索引转换为列:

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

Or use DataFrame.merge with default inner join:或者使用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

You can use merge to check it like this:您可以像这样使用合并来检查它:

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 contains not missing terms in ig.合并在 ig 中不包含缺失的术语。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从数据框中获取列中唯一值的最后一行 - Pandas - Get the last rows for a unique value in a column from a dataframe - Pandas Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column 在Pandas数据框中将行折叠为一列值 - Collapsing rows into one column value in pandas dataframe 大熊猫:通过列的值提取某些行作为数据框 - pandas: extract certain rows as a dataframe by the value of a column 根据列值重复 pandas DataFrame 中的行 - Repeat rows in a pandas DataFrame based on column value 在pandas数据框中对行进行排序并获取列ID - Sort rows and get column IDs in a pandas dataframe Pandas:如何在由另一列分组的列上获取具有最大值 value_count 的行作为数据框 - Pandas: how to get the rows that has the maximum value_count on a column grouping by another column as a dataframe Pandas DataFrame 按列值组合行,其中日期行是 NULL - Pandas DataFrame combine rows by column value, where Date Rows are NULL 对数据帧的行进行排序并获取熊猫数据帧中的列值 - Sort the rows of a dataframe and get the column values in pandas dataframe 根据一列过滤熊猫数据框:保留所有行(如果值是该列) - Filter pandas dataframe based on a column: keep all rows if a value is that column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM