Arvind Sudheer
Arvind Sudheer

Reputation: 123

How do I iterate through a dataframe's columns and then run the describe() function on each of these columns?

What I have tried so far is to iterate through the dataframe columns and then use the control variable in the df.<columnName>.describe() function, but this doesn't seem to work...

for (columnName, columnData) in df.iteritems():
  print('Column Name: {}'.format(columnName))
  column_stats = df.columnName.describe()

This is the output :

attr__ return object.getattribute(self, name) AttributeError: 'DataFrame' object has no attribute 'columnName'

Can someone help me with this rather trivial doubt ...

Upvotes: 0

Views: 92

Answers (1)

Renaud
Renaud

Reputation: 2819

Try it:

for columnName, columnData in df.iteritems():
    print('Column Name: {}'.format(columnName))
    column_stats = df[columnName].describe()

df.columnName is searching for a Column Name ColumnName not the value of your variable

Upvotes: 2

Related Questions