M Trette M Trette - 1 year ago 140
Python Question

Loop over multiple columns in dataframe

I have a dataframe from a CSV file that has 61 columns and 1mil rows. 25 of those columns

(Flag_1, Flag_2, ..., Flag_25)
as values for each row of the dataframe.

What I'm trying to do is loop through each column to determine if there is a True for that entire row within those columns, I just need a minimum of one true. If there is a True then a new column,
will have a True value for that row, if not, then False.

I can use the for loop for a single column like so,

for index, x in data2['FLAG_1'].iteritems() :
data2['FLAG_ALL'] = data2['FLAG_1'] == True

but can't figure out for multiple columns.

Answer Source

Please try:

data2['FLAG_ALL'] = data2.any(axis=1,bool_only=True).values 

More info on any() http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.any.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download