0

Can the FOR loop structure point to a single column of data within a Pandas Dataframe?

I have imported data from EXCEL as a Dataframe using Pandas. Presently, I convert a single Column of that Dataframe to a LIST because a list seems to be required for the application of the << for .... in .....>> loop structure. This approach works, but of course it also takes extra lines of code, which I would like to avoid.

Birdman
  • 11
  • 5
  • Generally looping and pandas don't go together but `for v in df['col']:` should work, no? Series are most certainly iterable. Why do you think that converting to a list is required? – Henry Ecker Aug 07 '21 at 02:09
  • I also thought that Looping and Pandas didn't work together, that Loops only worked with Lists. But (importantly) I am a newbie to both Python and Pandas so rather than attempt the impossible, and to do so by totally inane syntax, I ask questions first! I was also concerned by my column's datatype (mixed alpha and numeric, which is converted to bits during a serial.write() command. As you have suggested, I did throw out the conversion to LIST, and drew my data directly from the Dataframe column with the syntax you suggest and YES! it works! – Birdman Aug 07 '21 at 02:38
  • pandas and loops don't "go together" and don't "work together" are actually different things. Generally this refers to how slow looping operations are compared to many of the faster operations available. This does not mean that pandas objects cannot be iterated normally. [Answer explaining timings about looping](https://stackoverflow.com/a/55557758/15497888) – Henry Ecker Aug 07 '21 at 02:53
  • Henry: Thanks for the comments. What I meant by "go-" and "work-together" was whether the commands are compatible. I did not find examples of using FOR loop with Pandas dataframe. So I thought that perhaps the FOR loop would not be compatible with Pandas DF. Hope this explains where I was coming from. – Birdman Aug 08 '21 at 06:45

0 Answers0