1

Im getting the following error Shape of passed values is (1, 5), indices imply (5, 5). From what I can tell this suggests that the data set doesnt match the column count, and of course it obviously is correct. Initially I thought it could be due to using a list, but I get the same issue if passing in a numpy array.

Can anyone point out my stupidity, as im clearly doing something incorrectly.

data = ['data1', 'data2', 'data3', 'data4', 'data5']
report_name = 'test.csv'
try:
    df = pd.DataFrame(data, columns=['column1', 'column2', 'column3', 'column4', 'column5'], index=None)
    df.sort_values('column1', ascending=True, inplace=True)
    df.to_csv(report_name, index=False)
except Exception, e:
    print e

2 Answers 2

1

you have to pass a 2d dimensional array to pd.DataFrame for the data if you force the shape by passing columns

data = [['data1', 'data2', 'data3', 'data4', 'data5']]
df = pd.DataFrame(data, columns=['column1', 'column2', 'column3', 'column4', 'column5'])
Sign up to request clarification or add additional context in comments.

2 Comments

hmm, I attempted what I thought was this using a numpy array. Guess I mis understood numpy arrays too :(. Nice and simple when you know how :). thanks for pointing it out
@iNoob, it's just a detail because you try to create a DataFrame with 1 line, which is not very useful. In practice, you always pass a list of list or a 2d np array so you don't have to worry about it
0

You've missed the list brackets around data

df = pd.DataFrame(data = [data], columns=['column1', 'column2', 'column3', 'column4', 'column5'], index=None)

Things to note: pd.DataFrame() expects a list of tuples, this means:

data = ['data1', 'data2', 'data3', 'data4', 'data5']
df = pd.DataFrame(data)
# This implies every element in the list `data` is a tuple 
print(df)

Out[]:        0
         0  data1
         1  data2
         2  data3
         3  data4
         4  data5

As opposed to :

data = ['data1', 'data2', 'data3', 'data4', 'data5']
df = pd.DataFrame([data])
# This implies that the list `data` is the first tuple
print(df)
Out[]:        0      1      2      3      4
         0  data1  data2  data3  data4  data5

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.