How do you programmatically retrieve the number of columns in a pandas dataframe? I was hoping for something like:
df.num_columns
If the variable holding the dataframe is called df, then:
len(df.columns)
gives the number of columns.
And for those who want the number of rows:
len(df.index)
For a tuple containing the number of both rows and columns:
df.shape
df.info() function will give you result something like as below. If you are using read_csv method of Pandas without sep parameter or sep with ",".
raw_data = pd.read_csv("a1:\aa2/aaa3/data.csv")
raw_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5144 entries, 0 to 5143
Columns: 145 entries, R_fighter to R_age
#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))