dcu
dcu

Reputation: 347

Pandas dataframe transpose with column name instead of index throws ValueError

I am trying to show actual column name in json after dataframe has been transposed, below code works for LIMIT 3 in sql but fails if I try LIMIT 5 Any thoughts please?

from pandasql import *

pysqldf = lambda q: sqldf(q, globals())

q1 = """
SELECT
 beef as beef, veal as veal, pork as pork, lamb_and_mutton as lamb
FROM
 meat m
LIMIT 5;
"""

meat = load_meat()

df = pysqldf(q1)
#print(df.to_json(orient='records'))

hdf = pd.DataFrame(df)
print(hdf.T.reset_index().set_axis(range(len(hdf.columns)), axis=1, inplace=False).to_json(orient='records'))

ERROR

    'values have {new} elements'.format(old=old_len, new=new_len))
ValueError: Length mismatch: Expected axis has 6 elements, new values have 4 elements

Upvotes: 1

Views: 275

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

Try this:

df.T.reset_index()\
   .set_axis(range(len(df)+1), axis=1, inplace=False)\
   .to_json(orient='records')

Note: The issue is renaming the columns after the transpose, you need the length to be the number of rows in the original dataframe plus 1 for index.

Output:

'[{"0":"beef","1":0,"2":4,"3":8,"4":12,"5":16},{"0":"veal","1":1,"2":5,"3":9,"4":13,"5":17},{"0":"pork","1":2,"2":6,"3":10,"4":14,"5":18},{"0":"lamb","1":3,"2":7,"3":11,"4":15,"5":29}]'

Upvotes: 2

BENY
BENY

Reputation: 323226

After you T and reset_index , you have add one more columns , at the same time, the length of index is equal to columns before transposed, so you should using shape

print(hdf.T.reset_index().set_axis(range(hdf.shape[0]+1), axis=1, inplace=False).to_json(orient='records'))

Upvotes: 2

Related Questions