簡體   English   中英

ValueError:數組在Pandas中的長度必須相同

[英]ValueError: arrays must all be same length in Pandas

我遵循了Sentdex關於熊貓基礎的第二篇教程 ,並遇到了這個問題。 到目前為止,這是我的代碼:

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的值少於天數/訪問者數。 檢查您的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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM