ValueError: Shape of passed values is (1, 6), indices imply (6, 6)

Question:

I am passing a list from flask function to another function, and getting this value error.

My code at sending end:

@app.route('/process', methods=['POST'])
def process():
    name = request.form['name']
    comment = request.form['comment']
    wickets = request.form['wickets']
    ga = request.form['ga']
    ppballs = request.form['ppballs']
    overs = request.form['overs']

    score = [name,comment,wickets,ga,ppballs,overs]
    results = []
    results = eval_score(score)
    print results

Receiver end :

def ml_model(data):
    col = pd.DataFrame(data,columns=['runs','balls', 'wickets', 'ground_average', 'pp_balls_left', 'total_overs'])
    predicted = predictor(col)

Trace of Error:

 ...
 line 1598, in dispatch_request
 return self.view_functions[rule.endpoint](**req.view_args)

 File "/Users/sbk/guestbook/guestbook.py", line 26, in process
 results = eval_score(score)

 File "/Users/sbk/guestbook/eval_score.py", line 6, in eval_score
 col = pd.DataFrame(data,columns=['runs','balls', 'wickets',  'ground_average', 'pp_balls_left', 'total_overs'])

 File "/Users/sbk/anaconda2/lib/python2.7/site-  packages/pandas/core/frame.py", line 385, in __init__
 copy=copy)

 File "/Users/sbk/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 533, in _init_ndarray
 return create_block_manager_from_blocks([values], [columns, index])

 File "/Users/sbk/anaconda2/lib/python2.7/site-packages/pandas/core/internals.py", line 4631, in  create_block_manager_from_blocks
 construction_error(tot_items, blocks[0].shape[1:], axes, e)

 File "/Users/sbk/anaconda2/lib/python2.7/site-packages/pandas/core/internals.py", line 4608, in construction_error
 Open an interactive python shell in this framepassed, implied))

Please let me know where I am going wrong.

Asked By: Sbk3824

||

Answers:

Simply change

col = pd.DataFrame(data, columns=['runs','balls', 'wickets', 'ground_average', 'pp_balls_left', 'total_overs'])

for

col = pd.DataFrame([data], columns=['runs','balls', 'wickets', 'ground_average', 'pp_balls_left', 'total_overs'])

You want [data] for pandas to understand they’re rows.


Simple illustration:

a = [1, 2, 3]
>>> pd.DataFrame(a)
   0
0  1
1  2
2  3

>>> pd.DataFrame([a])
   0  1  2
0  1  2  3
Answered By: rafaelc

I was having similar issues with making a dataframe from regressor coefficients (regressor.coeff_), and brackets gave another error asking for 2-d input. If you get this error, try appending the input array with [0] so it pulls the values out.
ex:
data[0]

Answered By: Candice Withrow

I was facing the similar error with the message

Shape of passed values is (68, 1783), indices imply (68, 68) in dataframe

And As per my guess, I fed the transpose of ndarray of data and that solved the problem

Changed from

Features_Dataframe = pd.DataFrame(data=Features, columns=Feature_Labels)  # here Features ndarray is 68*1783

To

Features_Dataframe = pd.DataFrame(data=Features.transpose(), columns=Feature_Labels)  # Now Features array became 1783*68 i.e., 1783 rows and 68 columns
Answered By: DevLoverUmar

Change this:

pd.DataFrame(regr.coef_, df_coeficientes.columns, columns=[‘Coeffecient’])

to this:

pd.DataFrame(regr.coef_[0], df_coeficientes.columns, columns=[‘Coeffecient’])

Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.