首页 > 解决方案 > 如何将单词列转换为标题列,将两篇文章的名称转换为行的标题?

问题描述

我写了一个代码,显示没有标题的数据框,只有数字

现在看起来像这样:

    1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17
0   0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.000000 0.000174   0.0 0.0 0.0 0.0 0.0 0.0 0.000000
1   0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.000087 0.000000   0.0 0.0 0.0 0.0 0.0 0.0 0.0000

我需要把它转换成那样(没有标题索引)

                word1 word2 word3..................................
    1987_1    data data .............

    1987_2    data data.............................

我写了这段代码来获取数据框:

# %matplotlib inline
import numpy as np
import pandas as pd
import sklearn as sk
import math 

data = pd.read_csv('D:\\Datasets\\dt.csv', index_col ="word")
dt=data.head(18)
# retrieving row by loc method
#first=data.iloc[:, [1]].values
first=dt["1987_1"]
second=dt["1987_2"]

row1=pd.DataFrame(first) 
row2=pd.DataFrame(second) 

def computeTF(article):
  tf = []
  for i in article: 
    cal=i/11463
    #i+=1
    #print(cal)
    tf.append(cal)
return tf

#running our sentences through the tf function:
#header = data.iloc[0]
tfFirst = computeTF(first)
tfSecond = computeTF(second)
#Converting to dataframe for visualization
dt.columns = dt.iloc[0]
print(dt)
tf_df= pd.DataFrame([tfFirst, tfSecond])
print(tf_df)

你有什么建议吗?

标签: pythonnumpymachine-learninganacondatf-idf

解决方案


推荐阅读