JavaEar 专注于收集分享传播有价值的技术资料

ValueError:数组在Pandas中的长度必须相同 (ValueError: arrays must all be same length in Pandas)

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

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.

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.

1个回答

    最佳答案

  1. 英文原文

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

    I.e. adding an item (an int i.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.

    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)
    

    中文翻译

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

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

     将pandas导入为pd
    将matplotlib.pyplot导入为plt
    来自matplotlib的进口风格
    style.use( 'ggplot')
    
    web_stats = {'Day':[1,2,3,4,5,6],
                 '访客':[43,43,34,45,64,34],
                 'Bounce_Rate':[65,72,62,64,54,66]}#根据评论复制教程中的值。
    
    df = pd.DataFrame(web_stats)
    
    打印(DF)
     

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

    I.e. adding an item (an int i.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.

    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)
    

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

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

     将pandas导入为pd
    将matplotlib.pyplot导入为plt
    来自matplotlib的进口风格
    style.use( 'ggplot')
    
    web_stats = {'Day':[1,2,3,4,5,6],
                 '访客':[43,43,34,45,64,34],
                 'Bounce_Rate':[65,72,62,64,54,66]}#根据评论复制教程中的值。
    
    df = pd.DataFrame(web_stats)
    
    打印(DF)