Pandas dataframe transpose with column name instead of index throws ValueError

↘锁芯ラ 提交于 2019-12-11 05:58:03

问题


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

回答1:


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'))



回答2:


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}]'


来源:https://stackoverflow.com/questions/56049101/pandas-dataframe-transpose-with-column-name-instead-of-index-throws-valueerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!