Python Pandas:将数据框合并到系列的索引和数据框的列上

时间:2020-02-23 19:37:39

标签: python python-3.x pandas dataframe

我有以下系列文章:

const iterable = {
  [Symbol.iterator]() {
    return {
      next() {
        return {} // <~ doesn't have done property - will cause infinite iteration
      }
    }
  }
}
console.log([...iterable])

和数据框:

>>>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

但是出现此错误:

ValueError:您正在尝试合并object和float64列。如果 您希望继续,应该使用pd.concat

1 个答案:

答案 0 :(得分:1)

您必须添加一些内容:

首先,您的counts_df没有列名,添加名称将为您提供一个具有列名的数据框

CapWord

您的合并现在有效。您应该删除不使用的列,并考虑是否要重复。如果您的counts_df已排序,那么合并将是这样。

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