如何在熊猫中合并四个表?

时间:2018-09-04 09:51:00

标签: python pandas

我有四个表:predicted_tagsactual_tagstags_namesnews_text

在表predicted_tagsactual_tags中,行名称是标签ID。在这些表中,1表示True,0表示False。

predicted_tagsactual_tags的形状为(23413,1369)。

predicted_tags

print(predicted_tags)
+-------+-----+---+-----+------+------+
|       |   1 | 3 | ... | 8345 | 8347 |
+-------+-----+---+-----+------+------+
| 35615 |   0 | 0 | ... |    1 |    0 |
| 58666 |   1 | 0 | ... |    0 |    0 |
| 16197 |   0 | 0 | ... |    0 |    1 |
| 68824 |   0 | 0 | ... |    1 |    1 |
| 22277 |   0 | 0 | ... |    1 |    0 |
+-------+-----+---+-----+------+------+

actual_tags

print(actual_tags)
+-------+-----+---+-----+------+------+
|       |   1 | 3 | ... | 8345 | 8347 |
+-------+-----+---+-----+------+------+
| 35615 |   0 | 0 | ... |    1 |    0 |
| 58666 |   1 | 1 | ... |    0 |    0 |
| 16197 |   0 | 0 | ... |    0 |    1 |
| 68824 |   0 | 0 | ... |    1 |    1 |
| 22277 |   0 | 1 | ... |    1 |    0 |
+-------+-----+---+-----+------+------+

tags_names

print(tags_names)
+--------+----------+-------------+
|        |   tag_id |  tag_name   |
+--------+----------+-------------+
| 127579 |        1 | politics    |
| 108814 |        3 | economics   |
|    ... |      ... | ...         |
|     18 |     8345 | hot         |
| 257141 |     8347 | environment |
+--------+----------+-------------+

news_text

print(news_text)
+----------+------------------------+-----------------------------+
|          |       news_name        |         news_content        |
+----------+------------------------+-----------------------------+
|    35615 | Secret of…             |  Hi! Today I will talk...   |
|    58666 | Conversations with a … |  I have a big experience... |
|    16197 | Harm of alcohol        |  Today, we…                 |
|      ... | ...                    |  ...                        |
|    68824 | Hot news               |  Celebrity with...          |
|    22277 | Finance market         |  Last week…                 |
+----------+------------------------+-----------------------------+

我想要下一张桌子:

+-------+------------------------+----------------------------+------------------------+---------------------------+
|       |       news_name        |        news_content        |     predicted_tags     |        actual_tags        |
+-------+------------------------+----------------------------+------------------------+---------------------------+
| 35615 | Secret of…             | Hi! Today I will talk...   | ['hot']                | ['hot']                   |
| 58666 | Conversations with a … | I have a big experience... | ['politics']           | ['politics', 'economics'] |
| 16197 | Harm of alcohol        | Today, we…                 | ['environment']        | ['environment']           |
| 68824 | Hot news               | Celebrity with...          | ['hot', 'environment'] | ['hot', 'environment']    |
| 22277 | Finance market         | Last week…                 | ['hot']                | ['hot', 'economics']      |
+-------+------------------------+----------------------------+------------------------+---------------------------+

如何使用熊猫来做到这一点?

3 个答案:

答案 0 :(得分:2)

首先,创建一个包含所有实际/预测值的列,例如:

predicted_tags['pred_loc'] = predicted_tags.values.tolist()
actual_tags['actual_loc'] = actual_tags.values.tolist()

此外,如果您的tag_id(在tag_names dataFrame中)与实际和预测标签dataFrame中的列的顺序相同。然后,只需创建一个标签名称列表,例如

tags = tag_names.tag_name.values.tolist()

现在,在进行转换之前,我们将其合并到news_text dataFrame

news_text = news_text.merge(predicted_tags['pred_loc'], how='outer', left_index=True, right_index=True)
news_text = news_text.merge(actual_tags['actual_loc'], how='outer', left_index=True, right_index=True)

现在,我们进行转换:

news_text.pred_loc = news_text.pred_loc.apply(lambda x: [tags[i] for i, j in enumerate(x) if j == 1])
news_text.actual_loc = news_text.actual_loc.apply(lambda x: [tags[i] for i, j in enumerate(x) if j == 1])

答案 1 :(得分:2)

您可以使用pandas apply将标签的一种热编码转换为标签列表。我会将tag_names从数据帧修改为一系列(其索引是tag_id,值是标签名)。我现在仅用两个标签进行演示。

>>> import pandas as pd
>>> df = pd.DataFrame({
            1: [0, 1, 0, 0, 0],
            3: [0, 1, 0, 0, 1]}, 
        index=[35615, 58666, 16197, 68824, 22277] ) # predicted_tags
>>> df
       1  3
35615  0  0
58666  1  1
16197  0  0
68824  0  0
22277  0  1
>>> tag_names = pd.DataFrame({"tag_id": [1,3,], 
        "tag_name": ["politics", "economics"]},
         index=[127579, 108814])
>>> tag_names
        tag_id   tag_name
127579       1   politics
108814       3  economics
>>> tags = tag_names.set_index("tag_id").tag_name
>>> tags
tag_id
1     politics
3    economics
Name: tag_name, dtype: object
>>> df.apply( lambda row: [tags.loc[k] for k,v in row.items() if v > 0] , axis=1)
35615                       []
58666    [politics, economics]
16197                       []
68824                       []
22277              [economics]
dtype: object
>>> 

您现在应该可以将其与索引上的news_text一起加入

答案 2 :(得分:2)

tags_names df转换为字典,并使用它重命名列:

tag_names = dict(zip(tags_names['tag_id'], tags_names['tag_names']))

predicted_tags.rename(columns = tag_names, inplace = True)
actual_tags.rename(columns = tag_names, inplace = True)

获取值为1的列名。

news_text['actual_tags'] = (actual_tags == 1 ).apply(lambda y: actual_tags.columns[y.tolist()].tolist(), axis=1)
news_text['predicted_tags'] = (predicted_tags == 1 ).apply(lambda y: predicted_tags.columns[y.tolist()].tolist(), axis=1)