Python Forum

Full Version: How to use the count function from an Excel file using Python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone - I am in the early stages of learning Python and decided to start with using Excel in Python. I am running the code below in Jupyter and it works well. This notebook currently has a state column and a country column. I would like to count the total number of states but I don't know how to reference the columns - could anyone please tell me how to start? I tried the second code below but that did not run.

import pandas as pd

df = pd.read_excel (r'C:\Users\Family\Desktop\Anaconda\Book1.xlsx')
print (df)
st = 'State'.count()
You most always have have reference to the DataFrame,which now stored in variable df.
print(df['State'].count())
Thank you for the guidance and reading, I appreciate it.