Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Output from apply lambda function with groupby is object, want to convert to dataframe

I have a dataframe, which i applied lambda funciton with groupby, the lambda function generated two values as output, and the output is object data type.

I tried with output.to_frame().reset_index() and pd.DataFrame(output), neither really worked.

output.to_frame().reset_index() made the second column name 0, but i can’t display the second column with output.0. After output.to_frame().reset_index() looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

What’s the best way to convert this object output from lambda function with groupby to a dataframe looks like this?

enter image description here

>Solution :

It would be better to do it upfront (before/when using groupby) but you can still try this :

out = df.join(pd.DataFrame(df.pop("0").tolist(), columns=["score", "value"]))


Output :

print(out)

  id  score  value
0  a      1      2
1  b      4      5
2  c      6      7
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading