Convert Pandas Dataframe To Python List
I have the following dataframe: In [137]: counts Out[137]: SourceColumnID 3029903181 3029903182 3029903183 3029903184 ResponseCount ColID QuestionID Ro
Solution 1:
I believe you want counts.reset_index().to_dict('records')
.
Using 'records'
with to_dict
makes it give you a list of dicts, one dict per row, which is what you want. You need to use reset_index()
to get the index information in as columns (because 'records' throws away the index). Conceptually, the dicts you say you want don't distinguish between what's in the index of your pivot table and what's in the columns (you just want all index and column labels as keys in the dict), so you need to reset_index
to remove the index/column distinction.
Post a Comment for "Convert Pandas Dataframe To Python List"