将单行DataFrame对象处理为6x6 DataFrame

时间:2018-10-09 13:09:55

标签: python dataframe

我想做的就是标题中的详细解释,但是在很大程度上,这是我的问题:

为示例起见,假设我有一个包含36个问题的Google表单,并且我想使用Python 3处理该行数据框的答案。问题是我遇到了一个错误,但我正在前进自己。这是我尝试过的:

from flask import Flask, render_template, request
import pandas as pd
import numpy as np

io_table=pd.DataFrame(np.random.random_sample((1,36)))
fctr_column=pd.DataFrame(np.random.random_sample((6)))

io_table=pd.DataFrame(io_table) #Convert list to DataFrame


io_t=io_table
factor=fctr_column
test=pd.DataFrame()

for i in range(0,io_table.shape[1]+1):
    test=io_table.loc[0,i+1:i+6], ignore_index=True
    i=i+6
    print(test)

而且,正如我之前提到的,我遇到了一个错误:

  File "path/to/temp.py", line 29, in <module>
    test=io_table.loc[0,i+1:i+6], ignore_index=True

TypeError: cannot unpack non-iterable bool object

现在,我不知道该怎么办。谁能提供解决方案?

  

编辑:预期的输入和输出   enter image description here

1 个答案:

答案 0 :(得分:1)

不确定我是否正确,但是如果您有一个包含36个值的DataFrame,则可以使用以下示例中的内容对其进行重塑:

import pandas as pd

a = range(1, 37)

df = pd.DataFrame(a).T

df.values.reshape((6,6))
#[[ 1  2  3  4  5  6]
# [ 7  8  9 10 11 12]
# [13 14 15 16 17 18]
# [19 20 21 22 23 24]
# [25 26 27 28 29 30]
# [31 32 33 34 35 36]]