How do I retrieve the number of columns in a Pandas data frame?

后端 未结 9 2114
心在旅途
心在旅途 2021-01-29 18:06

How do you programmatically retrieve the number of columns in a pandas dataframe? I was hoping for something like:

df.num_columns
9条回答
  •  抹茶落季
    2021-01-29 19:12

    #use a regular expression to parse the column count
    #https://docs.python.org/3/library/re.html
    
    buffer = io.StringIO()
    df.info(buf=buffer)
    s = buffer.getvalue()
    pat=re.search(r"total\s{1}[0-9]\s{1}column",s)
    print(s)
    phrase=pat.group(0)
    value=re.findall(r'[0-9]+',phrase)[0]
    print(int(value))
    

提交回复
热议问题