【发布时间】:2017-11-16 22:29:12
【问题描述】:
我在关注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
>>>
任何帮助将不胜感激。
【问题讨论】:
-
Bounce_Rate 只有 5 个值,您必须传递相同长度的数组才能创建数据帧
-
不确定,但
Bounce_Rate只有五个值,其他有六个。 -
如果有帮助,请考虑将答案标记为正确。欢迎来到 SO!
标签: python pandas valueerror