python - 值错误 : arrays must all be same length - print dataframe to CSV

标签 python windows pandas csv dataframe

感谢您的光临!我希望得到一些帮助使用 pandas 数据框创建 csv。这是我的代码:

a = ldamallet[bow_corpus_new[:21]]
b = data_text_new

print(a)
print("/n")
print(b)

d = {'Preprocessed Document': b['Preprocessed Document'].tolist(), 
     'topic_0': a[0][1], 
     'topic_1': a[1][1], 
     'topic_2': a[2][1], 
     'topic_3': a[3][1], 
     'topic_4': a[4][1], 
     'topic_5': a[5][1], 
     'topic_6': a[6][1], 
     'topic_7': a[7][1], 
     'topic_8': a[8][1], 
     'topic_9': a[9][1], 
     'topic_10': a[10][1],
     'topic_11': a[11][1], 
     'topic_12': a[12][1],
     'topic_13': a[13][1],
     'topic_14': a[14][1],
     'topic_15': a[15][1],
     'topic_16': a[16][1],
     'topic_17': a[17][1],
     'topic_18': a[18][1],
     'topic_19': a[19][1]}

print(d)

df = pd.DataFrame(data=d)
df.to_csv("test.csv", index=False)

数据:

print(a): 格式为元组

[[(主题数: 0, 主题百分比),...(19, #)], [(下一行的主题分布, #)...(19, .819438),...(# ,#),...]

print(a)

打印(b)

print(b)

这是我的错误: the error

这是数据框的大小:

The size of b

the shape of a

shape of a

这是我希望的样子: the dream

任何帮助将不胜感激:)

最佳答案

为它自己的列表中的所有行获取每个元组的第二个值可能是最简单的。像这样

topic_0=[]
topic_1=[]
topic_2=[]
...and so on
for i in a:
    topic_0.append(i[0][1])
    topic_1.append(i[1][1])
    topic_2.append(i[2][1])
    ...and so on

然后你可以像这样制作你的字典

d = {'Preprocessed Document': b['Preprocessed Document'].tolist(), 
     'topic_0': topic_0, 
     'topic_1': topic_1, 
      etc. }

关于python - 值错误 : arrays must all be same length - print dataframe to CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55639143/

相关文章:

python - 选择两个给定日期之间的 pandas 数据框,其中两列的值相等

python - 如何使用 numpy 分别计算每年的平均月温度?

windows - shell 扩展会导致我的程序崩溃吗?

c++ - 如何在写入发生之前获得文件更改的通知?

Python 在 Windows 上的 os.listdir 行为

python - 如何从执行进程任务运行 py 文件?

c++ - 查看winapi函数是否可用

python Dataframe 与过滤器的差异在一行中

python - 属性错误 : 'DataFrame' object has no attribute 'get_dtype_counts'

python - 可视化两个数值数组之间的差异