使用不完整数据填充Pandas DataFrame

时间:2018-05-14 02:21:58

标签: python python-3.x pandas

我正在尝试为Anova测试创建一个F表。我的价值已经是这样的:

data = [[ssBetween,ssWithin,ssTotal],[dfbetween,dfwithin,dftotal], [msBetween,msWithin],[F]]
index = ['Between','Total','Within']
columns = ['SS','df','MS','F']
df = pd.DataFrame(index=index,columns=columns)
df['SS'] = data[0]
df['df'] = data[1]
df['MS'] = data[2]
df['F'] = data[3]

当我跑步时我得到这个错误:

ValueError:值的长度与索引的长度

不匹配

我知道我可以用零或其他东西填写缺失的值,但我想学习更多的pythonic方法来做到这一点。我只想用我的值填充表格,并将表格中的其余空格留作空值。

2 个答案:

答案 0 :(得分:1)

只需使用添加数据

pd.DataFrame(data = data,index = columns,columns = index).T
Out[149]: 
                SS         df         MS     F
Between  ssBetween  dfbetween  msBetween     F
Total     ssWithin   dfwithin   msWithin  None
Within     ssTotal    dftotal       None  None

答案 1 :(得分:0)

您可以使用from_dict()

df = pd.DataFrame.from_dict( {k: v for k,v in zip(columns, data)}, orient='index').T
df.index = index

        SS          df          MS          F
Between ssBetween   dfbetween   msBetween   F
Total   ssWithin    dfwithin    msWithin    None
Within  ssTotal     dftotal     None        None