【问题标题】:match index from pyspark dataframe in pandaspandas中pyspark数据框的匹配索引
【发布时间】:2018-07-21 22:46:46
【问题描述】:

我有以下 pyspark 数据框 (testDF=ldamodel.describeTopics().select("termIndices").toPandas())

topic|    termIndices|         termWeights|
+-----+---------------+--------------------+
|    0|    [6, 118, 5]|[0.01205522104545...|
|    1|   [0, 55, 100]|[0.00125521761966...|

我有以下单词列表

['one',
 'peopl',
 'govern',
 'think',
 'econom',
 'rate',
 'tax',
 'polici',
 'year',
 'like',
........]

我正在尝试将 vocablist 匹配到 termIndicestermWeights

到目前为止,我有以下内容:

for i in testDF.items():
    for j in i[1]:
        for m in j:
            t=vocablist[m],m
            print(t)

导致:

('tax', 6)
('insur', 118)
('rate', 5)
('peopl', 1)
('health', 84)
('incom', 38)
('think', 3)
('one', 0)
('social', 162)
.......

但我想要类似的东西

('tax', 6, 0.012055221045453202)
('insur', 118, 0.001255217619666775)
('rate', 5, 0.0032220995010401187)

('peopl', 1,0.008342115226031033)
('health', 84,0.0008332053105123403)
('incom', 38, ......)

任何帮助将不胜感激。

【问题讨论】:

    标签: list pandas pyspark


    【解决方案1】:

    我建议您将lists 向下传播到termIndicestermWeights 列中。完成此操作后,您实际上可以map 为其术语名称编制索引,同时使术语权重与每个术语索引保持一致。下图为:

    df = pd.DataFrame(data={'topic': [0, 1],
                            'termIndices': [[6, 118, 5],
                                            [0, 55, 100]],
                            'termWeights': [[0.012055221045453202, 0.012055221045453202, 0.012055221045453202],
                                            [0.00125521761966, 0.00125521761966, 0.00125521761966]]})
    
    dff = df.apply(lambda s: s.apply(pd.Series).stack().reset_index(drop=True, level=1))
    
    vocablist = ['one', 'peopl', 'govern', 'think', 'econom', 'rate', 'tax', 'polici', 'year', 'like'] * 50
    
    dff['termNames'] = dff.termIndices.map(vocablist.__getitem__)
    
    dff[['termNames', 'termIndices', 'termWeights']].values.tolist()
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多