Should I leave out bad undergraduate research experiences? I am a mathematics and physics double major student entering my third year. Over this summer and the previous semester I have been working with a research group in physics writing simulation code. But I have been taking an incredibly long time to finish writing this objectively simple code that I have become very dissatisfied and pessimistic about my future in physics. I have been thinking about applying to graduate school in mathematics instead but I wonder: Would it even help to list this in my graduate school applications? I've spent a lot of time on this project yet I have near nothing to show for it. No publications or even interesting results. My code isn't done yet! Should I bother asking for a letter of recommendation from my PI knowing I did a terrible job at it? I have good grades but I do not know anyone who I believe will give me a strong letter of recommendation, which I know is crucial to admissions.
Make sure you have some good talking points about the experience and then keep it on your resume. (My opinion as someone who has reviewed resumes for the company I work for, granted graduate school apps may be different.)

What did you learn about the subject matter or about yourself? What struggles did you overcome or how would you do certain things differently? Is there anything that went well, or that you could spin positively?

I would see it as a bigger red flag if I saw your resume and thought you did nothing over the summer/semester than if I saw you worked and didn't have publications (that isn't an uncommon experience).  

As for the letter, you're going to have to make that call based on your relationship with your PI.  If you're really struggling with the decision, just ask your PI about your performance and what they thought you did well/could improve on - that could be telling as to what they would write.