fork download
  1. #%% Importing Modules
  2.  
  3. import pandas as pd
  4.  
  5. #%% Sample DataFrame
  6.  
  7. df = pd.DataFrame({
  8. 'first_name': ['alice','bob','charlie','daisy'],
  9. 'last_name':['smith','jones','joneson','white'],
  10. 'age': [25,20,30,35]})
  11.  
  12. #%% Method 1 : Long and Slow
  13.  
  14. emptyList = []
  15.  
  16. for colName in df.columns.values:
  17.  
  18. emptyList2 = []
  19.  
  20. for index, row in df.iterrows():
  21.  
  22. if len(str(row[colName])) != None:
  23. charLength = len(str(row[colName]))
  24. else:
  25. charLength = 0
  26.  
  27. emptyList2.append(charLength)
  28.  
  29. emptyList.append((colName,max(emptyList2)))
  30.  
  31.  
  32. maxChar = dict(emptyList)
  33.  
  34. #%% Method 1 : Short and Fast
  35.  
  36. maxChar = [(colName, df[colName].apply(lambda x: len(str(x)) if x!=None else 0).max())for colName in df.columns.values]
Success #stdin #stdout 0.52s 60508KB
stdin
Standard input is empty
stdout
Standard output is empty