title: How to get maximum length of each column in the data frame using pandas python
body: <p>I have a data frame where most of the columns are varchar/object type. Length of the column varies a lot and could be anything within the range of 3 - 1000+ . Now, for each column, I want to measure maximum length.</p>
  
  <p>I know how to calculate maximum length for a col. If its varchar then:</p>
  
  <pre><code>max(df.char_col.apply(len))
  </code></pre>
  
  <p>and if its number (float8 or int64) then:</p>
  
  <pre><code>max(df.num_col.map(str).apply(len))
  </code></pre>
  
  <p>But my dataframe has hundreds of column and I want to calculate maximum length for all columns at the same time. The problem for that is, there are different data types, and I dont know how to do all at once. </p>
  
  <blockquote>
    <p>So Question 1: How to get maximum column length for each columns in the data frame</p>
  </blockquote>
  
  <p>Now I am trying to do that only for varchar/object type columns using following code:</p>
  
  <pre><code>xx = df.select_dtypes(include = ['object'])
  for col in [xx.columns.values]:
     maxlength = [max(xx.col.apply(len))]
  </code></pre>
  
  <p>I selected only object type columns and tried to write a for loop. But its not working. probably using apply() within for loop is not a good idea.</p>
  
  <blockquote>
    <p>Question 2: How to get maximum length of each column for only object type columns</p>
  </blockquote>
  
  <p>Sample data frame:</p>
  
  <pre><code>d1 = {'name': ['john', 'tom', 'bob', 'rock', 'jimy'], 'DoB': ['01/02/2010', '01/02/2012', '11/22/2014', '11/22/2014', '09/25/2016'], 'Address': ['NY', 'NJ', 'PA', 'NY', 'CA'], 'comment1': ['Very good performance', 'N/A', 'Need to work hard', 'No Comment', 'Not satisfactory'], 'comment2': ['good', 'Meets Expectation', 'N', 'N/A', 'Incompetence']}
  df1 = pd.DataFrame(data = d1)
  df1['month'] = pd.DatetimeIndex(df1['DoB']).month
  df1['year'] = pd.DatetimeIndex(df1['DoB']).year
  </code></pre>