ValueError:Pandas 中的数组长度必须相同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47340189/
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-14 04:46:57  来源:igfitidea点击:

ValueError: arrays must all be same length in Pandas

pythonpandasvalueerror

提问by Terry T.

I was following Sentdex's second tutorial on pandas basics, and ran into this problem. Here's my code up to this point:

我正在关注Sentdex 关于Pandas基础知识的第二个教程,并遇到了这个问题。到目前为止,这是我的代码:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')

web_stats = {'Day' : [1,2,3,4,5,6],
             'Visitors' : [43,53,34,45,64,34],
             'Bounce_Rate' : [65,72,62,64,66]}

df = pd.DataFrame(web_stats)

print(df)

I was really confused when this error message popped up. Please note that I'm on a mac.

当这个错误信息弹出时,我真的很困惑。请注意,我使用的是 mac。

Traceback (most recent call last):
  File "/Users/Terry/Documents/df.py", line 10, in <module>
    df = pd.DataFrame(web_stats)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/frame.py", line 275, in __init__
    mgr = self._init_dict(data, index, columns, dtype=dtype)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/frame.py", line 411, in _init_dict
    return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/frame.py", line 5496, in _arrays_to_mgr
    index = extract_index(arrays)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/frame.py", line 5544, in extract_index
    raise ValueError('arrays must all be same length')
ValueError: arrays must all be same length
>>> 

Any help would be much appreciated.

任何帮助将非常感激。

回答by alexisdevarennes

Bounce_Rate has less values than the amount of days / visitors. Check your web_stats dictionary

Bounce_Rate 的值小于天数/访问者的数量。检查您的 web_stats 字典

I.e. adding an item (an inti.e. a number) to that list would fix it. But make sure, you notice in the below example I change the list values to [65, 72, 62, 64, 54, 66] according to comments. You will probably want to check which values should go there.

即添加一个项目(int即一个数字)到该列表将修复它。但是请确保您注意到在下面的示例中我根据注释将列表值更改为 [65, 72, 62, 64, 54, 66] 。您可能想要检查哪些值应该放在那里。

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')

web_stats = {'Day' : [1,2,3,4,5,6],
             'Visitors' : [43,53,34,45,64,34],
             'Bounce_Rate' : [65, 72, 62, 64, 54, 66]}  # Copied values from tutorial according to comments.

df = pd.DataFrame(web_stats)

print(df)