Pandas 错误“***ValueError:长度不匹配:预期轴有 0 个元素,新值有……”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22868458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:53:43  来源:igfitidea点击:

Pandas error "***ValueError: Length mismatch: Expected axis has 0 elements, new values have..."

pythonpandas

提问by DrMisha

I am getting an error when accessing data within a Pandas DataFrame df:

在 Pandas DataFrame df 中访问数据时出现错误:

ipdb> df.loc[1988, 'ATTEND']
*** ValueError: Length mismatch: Expected axis has 0 elements, new values have 1481 elements

1988 and 'ATTEND' are both in df:

1988 和 'ATTEND' 都在df 中

ipdb> 1988 in df.index
True
ipdb> 'ATTEND' in df.columns
True

I have no idea what this error is about. Any thoughts?

我不知道这个错误是关于什么的。有什么想法吗?

Thanks in advance.

提前致谢。

Example:

例子:

ipdb> df.iloc[21875:21880, 277:280]
      FUND  ATTEND  MAATTEND
1988     3       1         4
1988     2       3         0
1988     2       2         2
1988     2       2         7
1988     2       3         2

ipdb> df.loc[1988, 'ATTEND']
*** ValueError: Length mismatch: Expected axis has 0 elements, new values have 1481 elements


GSSFilename = 'GSS Dataset/GSS7212_R2_copy.sav'
data = srw.SavReader(pathToData + GSSFilename)
df = pd.DataFrame(data.all(), index=data[:,0],    columns=ALL_VARIABLE_NAMES)
with data: 
    data = np.array(data.all()) 

UPDATE: I think this problem was caused by two different Python instances accessing the same .sav file.

更新:我认为这个问题是由两个不同的 Python 实例访问同一个 .sav 文件引起的。

采纳答案by DrMisha

I think this problem was caused by two different Python instances accessing the same .sav file

我认为这个问题是由两个不同的 Python 实例访问同一个 .sav 文件引起的