Code Sample, a copy-pastable example if possible
for x in my_data_frame:
print rep(x)
Problem description
The current behavior is to print the columns during the normal iteration of a data frame. However, most of the time when actually using a DataFrame we want to iterate over the rows. How useful in data analysis is it to iterate over the columns? In my nearly 2 years of using pandas, not once have I had to do this. If one needs this, my_data_frame.columns
works just fine.
In addition, there is no my_data_frame.rows
to work on. The only good way to iterate over rows is to use iterrows:
for index, row in my_data_frame.iterrows():
print row
This seems like something that should be much more obvious than it is, and to even find that answer, one would pretty much need to google and find the stack overflow answer.
Is there any way we could iterate over the rows when using the python in
syntax? I think this is a much more obvious thing to do during iteration than going through the columns for most users.
Expected Output
Row output using the python in
keyword.
Output of pd.show_versions()
Comment From: TomAugspurger
Docs are here: http://pandas-docs.github.io/pandas-docs-travis/basics.html#iteration
The analogy for DataFrame.__iter__
is like iterating over a dictionary (columns are the keys in this case).
You might not want .iterrows()
either, this converts the row to a Series. If you have heterogenous data this will force object
dtype. In practice, I find .itertuples()
most useful for row-wise iteration.
Even if we wanted to, this isn't something we could change without breaking a lot of people's code.
Comment From: jadolfbr
Thanks for the .itertuples()
suggestion - will definitely look into this.
I get the analogy, but I still think it is not very useful as the default iteration. The same analogy could be used for rows - IE each row being part of a list, like a list of dicts or named tupples. I code Rosetta, so I also get that people's code would break for this large change, but I've had my code break from pandas changes many times so far as well...