pd.DataFrame引发pandas错误('dataframe constructor未正确调用')

时间:2017-08-08 17:36:02

标签: python pandas

我是Python语言的新手,我必须用它来构建心理学实验。 我想使用Pandas来组合在我的实验的每次运行开始时创建的csv文件。但是我有这条错误消息:

CompMgFile1 = pd.DataFrame(CompMgFile1,colimns=['Amorce','Target'])
raise PandasError('DataFrame constructor not properly called!')
pandas.core.common.PandasError: DataFrame constructor not properly called!

我已经阅读了类似的主题,它可能是我的数据文件与参数不对应。但我不明白为什么因为,我的文件是在实验开始时创建的csv文件,我会认为它是一个数据帧没有?

这是我用于创建带有pandas的最终条件文件的创建文件的代码,以及用于生成此最终文件的代码。

import random 
import csv

我使用original_list.csv文件来定义我的单词而不是明确地执行它。我打开名为'original_list.csv'的文件,并将其内容读入变量'stimuli'

Stimuli=list(csv.reader(open('Original_list.csv',"rU"),delimiter=';'))

这个original_list文件就像那样(为了简化:M是我的Outgroup Primes,C是我的内群组,N是我的负面目标,P是我的正面目标

M1  C1  N1  P1
M2  C2  N2  P2
M3  C3  N3  P3
M4  C4  N4  P4
M5  C5  N5  P5
M6  C6  N6  P6
M7  C7  N7  P7
M8  C8  N8  P8
M9  C9  N9  P9
M10 C10 N10 P10

接下来,我将创建两个'空'变量,我将把单词列表

放入其中
AmorceMg=[]
AmorceCau=[]
TargetN=[]
TargetP=[]

接下来,我将遍历'stimuli'对象并将相应的单词放在各自的对象中

for i in range(len(Stimuli)):
    AmorceMg.append(Stimuli[i][0])
    AmorceCau.append(Stimuli[i][1])
    TargetN.append(Stimuli [i][2])
    TargetP.append(Stimuli [i][3])

现在我拿出标题行

AmorceMg=AmorceMg[1:]
AmorceCau=AmorceCau[1:]
TargetN=TargetN[1:]
TargetP=TargetP[1:]

然后我将它们随机化。这将为两个试验块创建我的4个随机单词列表

AmorceMgRand1=random.sample(AmorceMg,len(AmorceMg)) 
AmorceCauRand1=random.sample(AmorceCau,len(AmorceCau)) 
TargetNRand1=random.sample(TargetN,len(TargetN)) 
TargetPRand1=random.sample(TargetP,len(TargetP))

AmorceMgRand2=random.sample(AmorceMg,len(AmorceMg)) 
AmorceCauRand2=random.sample(AmorceCau,len(AmorceCau)) 
TargetNRand2=random.sample(TargetN,len(TargetN)) 
TargetPRand2=random.sample(TargetP,len(TargetP))

我定义'subjectID'和'sessionID'变量来命名订单文件

subjectID=expInfo['participant']
sessionID=expInfo['session']

现在我根据第一个和第二个块的兼容性创建了4个文件名

CompMgFile1=subjectID + '_' + sessionID + '_CompMg1.csv'
IncMgFile1=subjectID + '_' + sessionID + '_IncMg1.csv'
CompCauFile1=subjectID + '_' + sessionID + '_CompCau1.csv'
IncCauFile1=subjectID + '_' + sessionID + '_IncCau1.csv'

with open(CompMgFile1,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceMgRand1)): 
    writer.writerow([AmorceMgRand1[i],TargetNRand1[i]]) 

with open(IncMgFile1,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceMgRand1)): 
    writer.writerow([AmorceMgRand1[i],TargetPRand1[i]]) 

with open(CompCauFile1,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceCauRand1)): 
    writer.writerow([AmorceCauRand1[i],TargetPRand1[i]]) 

with open(IncCauFile1,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceCauRand1)): 
    writer.writerow([AmorceCauRand1[i],TargetNRand1[i]])

CompMgFile2=subjectID + '_' + sessionID + '_CompMg2.csv'
IncMgFile2=subjectID + '_' + sessionID + '_IncMg2.csv'
CompCauFile2=subjectID + '_' + sessionID + '_CompCau2.csv'
IncCauFile2=subjectID + '_' + sessionID + '_IncCau2.csv'

with open(CompMgFile2,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceMgRand2)): 
    writer.writerow([AmorceMgRand2[i],TargetNRand2[i]]) 

with open(IncMgFile2,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceMgRand2)): 
    writer.writerow([AmorceMgRand2[i],TargetPRand2[i]]) 

with open(CompCauFile2,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceCauRand2)): 
    writer.writerow([AmorceCauRand2[i],TargetPRand2[i]]) 

with open(IncCauFile2,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceCauRand2)): 
    writer.writerow([AmorceCauRand2[i],TargetNRand2[i]])

现在我根据每个块的先行变量创建两个文件名

Block1=subjectID + '_' + sessionID + '_order1.csv'
Block2=subjectID + '_' + sessionID + '_order2.csv'

现在我合并每个块的所有文件

import pandas as pd

CompMgFile1 = pd.DataFrame(CompMgFile1,columns=['Amorce', 'Target'])
IncMgFile1 = pd.DataFrame(IncMgFile1,columns=['Amorce', 'Target'])
CompCauFile1 = pd.DataFrame(CompCauFile1,columns=['Amorce', 'Target'])
IncCauFile1 = pd.DataFrame(IncCauFile1,columns=['Amorce', 'Target'])
CompMgFile2 = pd.DataFrame(CompMgFile2,columns=['Amorce', 'Target'])
IncMgFile2 = pd.DataFrame(IncMgFile2,columns=['Amorce', 'Target'])
CompCauFile2 = pd.DataFrame(CompCauFile2,columns=['Amorce', 'Target'])
IncCauFile2 = pd.DataFrame(IncCauFile2,columns=['Amorce', 'Target'])

merged = pd.concat([CompMgFile1, IncMgFile1, CompCauFile1,IncCauFile1])
merged.to_csv(Block1, index=None)

merged = pd.concat([CompMgFile2, IncMgFile2, CompCauFile2,IncCauFile2])
merged.to_csv(Block2, index=None) 

有人知道这个问题吗? 非常感谢您提前帮忙寻求帮助!

Ivane

0 个答案:

没有答案