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

时间:2017-11-16 22:29:12

标签: python pandas valueerror

我关注Sentdex's second tutorial on pandas basics,遇到了这个问题。到目前为止,这是我的代码:

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)

当弹出此错误消息时,我真的很困惑。请注意我在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
>>> 

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

Bounce_Rate的值少于天数/访客量。检查你的web_stats字典

即。将一个项目(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)

答案 1 :(得分:0)

当字典值的长度不一样时会出现这个错误,可以使用for循环检查key和对应值的长度

for key, value in web_stats.items(): print(key, len(value), sep=" | ")

输出将是:

日 | 6 访客 | 6 Bounce_Rate | 5

现在您知道 Bounce_Rate 的长度小于其他键