Giada Giada - 1 year ago 316
Python Question

Python ValueError: Shape of passed values is (1, 627), indices imply (3, 627)

I am trying to run this code:

import pandas as pd
import numpy as np

df = pd.read_csv('example.csv', sep=';', engine='python')
df1 =df.sort_values(['topic', 'student', 'level'], ascending=True)

count_list = df1.apply(lambda x: [df.ix[].student if >0 else np.nan, x.student, x.level>1], axis=1).values

#line giving the error
df1_count = pd.DataFrame(columns=['st_source','st_dest','reply_count'], data=count_list)

but constantly I get this error message:

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

Does anybody know how I can fix it?

Thank you!

Answer Source
count_list = df1.apply(lambda x: (df.ix[].student,np.nan,np.nan) if 0 else (np.nan, x.student, x.level>1), axis=1).values
df2 = pd.DataFrame(count_list)
df2[['st_source','st_dest','reply_count']] = df2[0].apply(pd.Series)
df2 = df2.drop(0, 1)

This will return a DataFrame like this:

   >>> df2
   st_source   st_dest reply_count
0  -0.689652       NaN         NaN
1   0.696232       NaN         NaN
2   0.767232       NaN         NaN
3        NaN  0.696232       False
4   1.024604       NaN         NaN
5   1.121045       NaN         NaN

Probably there is a better and more efficient way to do this, but this solves the issue. Notice I made your if statement to return a tuple of length 3 no matter which condition it fell into.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download