python - 如何将单词列转换为标题列,将两篇文章的名称转换为行的标题?
问题描述
我写了一个代码,显示没有标题的数据框,只有数字
现在看起来像这样:
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)
你有什么建议吗?
解决方案
推荐阅读
- asp.net-mvc - Automapper AddAfterMapAction 不调用方法
- android - gradlew testDebugUnitTest mergeAndroidReports
- python - 如何按 Telegram 中的文档描述进行过滤
- java - 将 Unicode 转换为 UTF-8
- python - Python; 求解等于零的方程
- android - 使 TextView "\n" 忽略段落创建
- c# - 从 ac# 库返回响应值到 VBA
- php - 在 Drupal 表单中呈现字段集的顺序
- javascript - 当 props 也被传递时,如何映射元素?
- docker - 在 docker 容器中使用凭据