PythonPandas:在序列索引和数据帧列上将数据帧合并到序列

2024-05-31 23:43:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下系列:

>>>counts = pd.Series({'0.0':5, '1.0':6, '2.0':14, '3.0':98})
>>>counts
0.0     5
1.0     6
2.0    14
3.0    98
dtype: int64

和数据帧:

>>>topic_keywords = [(0, 0.0, 'challenge, web, language, require, bot'),
                     (1, 3.0, 'time, huge, figure, image, run, develop'),
                     (2, 1.0, 'datum, user, access, speech, bandwidth'),
                     (3, 2.0, ' main, decide, audio, sensor, disabled, make'),
                     (4, 2.0, ' main, decide, audio, sensor, disabled, make'),
                     (5, 0.0, 'challenge, web, language, require, bot')]
>>> topicKeywordsDf = pd.DataFrame(topic_keywords, columns=['ID', 'Topic_Num', 'Topic_Keywords'])
>>> topicKeywordsDf = topicKeywordsDf.set_index('ID')
>>> topicKeywordsDf
    Topic_Num                                Topic_Keywords
ID
0         0.0        challenge, web, language, require, bot
1         3.0       time, huge, figure, image, run, develop
2         1.0        datum, user, access, speech, bandwidth
3         2.0   main, decide, audio, sensor, disabled, make
4         2.0   main, decide, audio, sensor, disabled, make
5         0.0        challenge, web, language, require, bot

我想合并序列上的数据帧,其中序列的索引将与数据帧的Topic_Num列匹配:

Topic_Num    Count    Topic_Keywords
0.0         5        challenge, web, language, require, bot
1.0         14       datum, user, access, speech, bandwidth
2.0         6        main, decide, audio, sensor, disabled, make
3.0         98       time, huge, figure, image, run, develop

优选地,最终数据帧应基于Topic_Num进行排序。如何合并这些

尝试:

counts_df = counts.to_frame()
merge = counts_df.merge(topicKeywordsDf, left_index=True, right_on="Topic_Num")

但是得到这个错误:

ValueError: You are trying to merge on object and float64 columns. If you wish to proceed you should use pd.concat


Tags: webtopicmakemainbotrequiresensorlanguage
1条回答
网友
1楼 · 发布于 2024-05-31 23:43:21

你需要补充几点:

首先,您的counts_df没有列名,添加该名称将得到一个带有列名的数据帧

counts_df=pd.DataFrame({'Topic_Num':counts.index, 'value':counts.values})

你的合并现在起作用了。你应该放弃你不使用的列,并考虑如果你想要重复。如果您的counts_df已排序,则合并将如此

merge = counts_df.merge(topicKeywordsDf, left_index=True, right_on="Topic_Num").drop_duplicates()

相关问题 更多 >