Python 将单行数据帧对象操作为6x6数据帧
我想做的事情在标题中有很好的解释,但我的问题是: 在本例中,假设我有一个包含36个问题的Google表单,我想使用Python3操作数据帧的那行答案。问题是我犯了一个错误,但我已经超越了自己。以下是我尝试过的:Python 将单行数据帧对象操作为6x6数据帧,python,dataframe,Python,Dataframe,我想做的事情在标题中有很好的解释,但我的问题是: 在本例中,假设我有一个包含36个问题的Google表单,我想使用Python3操作数据帧的那行答案。问题是我犯了一个错误,但我已经超越了自己。以下是我尝试过的: 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)))
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
文件“path/to/temp.py”,第29行,在
test=io_table.loc[0,i+1:i+6],ignore_index=True
TypeError:无法解压缩不可编辑的布尔对象
现在,我不知道该怎么办。有人能提供解决方案吗
编辑:预期的输入和输出
不确定我是否正确,但如果您有一个具有36个值的数据帧,您可以使用以下示例中的类似内容对其进行重塑:
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]]
你能显示预期的输出吗?@NagaKiran看我的更新。认为我必须使用循环是新手的错误。谢谢你,那就行了。