Python 如何合并熊猫中的四个表?
我有四个表:预测标记、实际标记、标记名称和新闻文本 在表中,预测的_标记和实际的_标记行名称是标记id。在这些表中,1表示真,0表示假 预测的_标签和实际的_标签的形状是234131369 预测的\u标签: 实际标签: 标签和名称: 新闻文本: 我想要下一张桌子:Python 如何合并熊猫中的四个表?,python,pandas,Python,Pandas,我有四个表:预测标记、实际标记、标记名称和新闻文本 在表中,预测的_标记和实际的_标记行名称是标记id。在这些表中,1表示真,0表示假 预测的_标签和实际的_标签的形状是234131369 预测的\u标签: 实际标签: 标签和名称: 新闻文本: 我想要下一张桌子: +-------+------------------------+----------------------------+------------------------+---------------------------+
+-------+------------------------+----------------------------+------------------------+---------------------------+
| | 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'] |
+-------+------------------------+----------------------------+------------------------+---------------------------+
如何使用Pandas实现这一点?首先,创建一列,其中包含所有实际/预测值,例如:
predicted_tags['pred_loc'] = predicted_tags.values.tolist()
actual_tags['actual_loc'] = actual_tags.values.tolist()
此外,如果tag_names数据框中的tag_id与实际和预测的tags数据框中的列的顺序相同。然后,只需创建一个标签名列表,如
tags = tag_names.tag_name.values.tolist()
现在,在转换之前,我们将把它合并到新闻文本数据框中
现在,我们转换为:
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])
首先,创建一个包含所有实际/预测值的列,例如:
predicted_tags['pred_loc'] = predicted_tags.values.tolist()
actual_tags['actual_loc'] = actual_tags.values.tolist()
此外,如果tag_names数据框中的tag_id与实际和预测的tags数据框中的列的顺序相同。然后,只需创建一个标签名列表,如
tags = tag_names.tag_name.values.tolist()
现在,在转换之前,我们将把它合并到新闻文本数据框中
现在,我们转换为:
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])
您可以使用将标记的一个热编码转换为标记列表。我将把tag_名称从dataframe修改为一个系列,其索引为tag_id,值为tag name。我现在只用两个标签来演示这一点
>>> 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
>>>
您现在应该可以将其与索引上的新闻文本连接起来。您可以使用将一个热编码标记转换为标记列表。我将把tag_名称从dataframe修改为一个系列,其索引为tag_id,值为tag name。我现在只用两个标签来演示这一点
>>> 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
>>>
您现在应该可以将其与索引上的新闻文本连接起来。将标签名称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)
将标记转换为字典,并使用它重命名列:
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)
这是你的任务,不是问题。到目前为止你试过什么?请阅读并编辑您的问题。我不知道如何转换标签名称列表并将其与表news\u text合并数据的大小是多少?我们可以写lambda让他们转换抱歉,我没有指出这个问题。我会更新我的问题。数据大小为20K行,包含预测的\u标记/实际的\u标记。这是您的任务,不是问题。到目前为止你试过什么?请阅读并编辑您的问题。我不知道如何转换标签名称列表并将其与表news\u text合并数据的大小是多少?我们可以写lambda让他们转换抱歉,我没有指出这个问题。我会更新我的问题。数据大小为20K行(预测的\u标记/实际的\u标记)。