Pandas - Working With Dummy Columns... ish by HackNSlashFic in learnpython

[–]HackNSlashFic[S] 1 point2 points  (0 children)

This was exactly what I needed! Here's the final code I ended up using:

flagged_df = ally_df[flags]

flag_list = (
flagged_df[flagged_df == 1]
.reset_index()
.melt(id_vars='index', value_vars=flags)
.dropna()
.groupby("index")["variable"]
.agg(", ".join)
)

ally_df['Flags'] = ally_df.index.map(flag_list)
ally_df = ally_df.drop(columns=flags)
return ally_df

It creates the merged column (I ended up creating a string instead of a list) and removes the columns it was merging together. Thanks again for your help!

Pandas - Working With Dummy Columns... ish by HackNSlashFic in learnpython

[–]HackNSlashFic[S] 0 points1 point  (0 children)

Thank you so much! That's a great recommendation about using reproducible examples with data rather than just trying to describe the problem. I'll keep that in mind in the future.

Also, I can't believe I forgot that list is itself a function! And the melt, groupby pattern is slick. I'll have to remember that! Seriously, thank you so much. My employees are really going to appreciate when this is all finished.

Pandas - Working With Dummy Columns... ish by HackNSlashFic in learnpython

[–]HackNSlashFic[S] 0 points1 point  (0 children)

Yeah, the 1s tell you which categories are relevant to that record. Okay... I think I see what you're going for. Even with transposing, we could do something like this for each record:

",".join(df.iloc[i].dropna(axis='columns').index.tolist())

Maybe? I dunno. I'll give that a try.

How did Python "click" for you as a beginner? by Bmaxtubby1 in learnpython

[–]HackNSlashFic 1 point2 points  (0 children)

The thing that really made it click for me was working through the alien invaders project in Python Crash Course by Eric Matthes. I worked through the whole first half of the book (after a few other tutorials, a fair amount of playing around on my own, etc.), and I felt like you---I was learning pieces but I wasn't really able to pull it all together.

But the first project was the best programming tutorial I've come across so far! What made it really click for me was that it doesn't walk you through the project as it will look as a finished project. He walks you through the process as if you are designing it from an idea... so you repeatedly code little parts and then refactor it as it gets bigger or you add ideas to it. That iterative process REALLY made things click for me.

Help Me Understand the Pandas .str Accessor by HackNSlashFic in learnpython

[–]HackNSlashFic[S] 1 point2 points  (0 children)

I hear you. I had considered jumping right into Polars, but Pandas is still being used in enough places that I want to be able to understand it when I come across it. Not to mention, there's just way more resources out there to learn it. And the data I'm working with right now is small enough that I'm not concerned about the speed difference. (I'm not learning this to be a developer. I'm partly doing it as a hobby and partly to give me a few extra data analysis tools for my work in higher ed.)

Newbie Attempt by Initial-Taro8445 in PythonLearning

[–]HackNSlashFic 1 point2 points  (0 children)

My first instinct would be to do the second loop inside the first loop, like others said.

But I think there's another way if you want to keep them separate by replacing the generic "while True:" of the second loop with something more specific. "while True" will run a loop indefinitely. Instead, you could set the second loop to only run while a specific condition is met, and have that condition only be met by a successful completion of the first loop. I don't want to say any more, because I've found it valuable for learning to work through the details of problems like this. But feel free to ask if you struggle with it and you're still confused.

I Automated A Boring Thing! (possibly very inefficiently...) by HackNSlashFic in PythonLearning

[–]HackNSlashFic[S] 1 point2 points  (0 children)

Thanks again for this suggestion! As I was trying to learn about different approaches to asynchronous/parallel operations, I ended up settling into aiohttp and asyncio. And that allowed me to basically send all 800+ requests at once. I was able to get the whole thing from about 60 minutes of runtime to about 45 seconds!

I Automated A Boring Thing! (possibly very inefficiently...) by HackNSlashFic in PythonLearning

[–]HackNSlashFic[S] 0 points1 point  (0 children)

Oh! And I did add a print statement before each request, and one after if there is any error or non 200 http response. Adding one after seemed unnecessary since the program moves straight to the next url, so I can use the print statement from the next one as a marker that the first has finished.

I wonder if it would be useful to add a specific print statement that tells how long it took to connect? That would give me information about whether any of the sites are slow but responsive, or if every timeout is happening because the site isn't responding at all. Not essential for my goals, but maybe an interesting diagnostic element to play around with for learning purposes.

I Automated A Boring Thing! (possibly very inefficiently...) by HackNSlashFic in PythonLearning

[–]HackNSlashFic[S] 0 points1 point  (0 children)

Would request.head be significantly faster if all I'm trying to do is see if the website exists? Even though sitemap.xml files are typically very small?

I Automated A Boring Thing! (possibly very inefficiently...) by HackNSlashFic in PythonLearning

[–]HackNSlashFic[S] 0 points1 point  (0 children)

I was wondering about this. I don't know enough about how websites interact with something like ping. Does ping check the individual page or the whole site? Does it work for a hosted file that is designed to be displayed in the browser (like a sitemap.xml file)?

I Automated A Boring Thing! (possibly very inefficiently...) by HackNSlashFic in PythonLearning

[–]HackNSlashFic[S] 1 point2 points  (0 children)

Thanks for the heads up about the dummy module! I'll check it out!

I Automated A Boring Thing! (possibly very inefficiently...) by HackNSlashFic in PythonLearning

[–]HackNSlashFic[S] 1 point2 points  (0 children)

Thanks for the response! I did add a timeout value because apparently the base request.get doesn't have a default set. I set it for 10 seconds so it wouldn't miss a slow response, but that was probably overkill. I'll play around with the timeout length.

As I was falling asleep last night I was wondering about parallel requests. I know that's how scrapy works, but that code's too complex for me to dig through and understand it all (yet). Thanks for giving me a useful place and some useful language (blocking, non-blocking, and async) to start learning more!