0

I've started using pandas this week so pardon me if I'm asking a really obvious question. I am trying to get pandas to loop through all the sheets in my Excel wb to extract a specific column, column F for counts of occurances via value_counts.

Previously I used value_counts and typed in the specific name of the worksheet and it worked in pulling out the value count of that 1 sheet. However, the minute I replaced the sheet name to df1, it stops working.

df = pd.ExcelFile("filepath.xlsx")
for df1 in df.sheet_names:
    df2 = pd.read_excel("filepath.xlsx", sheet_name=df1, usecols="F")
    df2.dropna(inplace=False)
    print (df2.value_counts())

I expect the output to be the unique values and their occurance numbers but it returns:

AttributeError: 'DataFrame' object has no attribute 'value_counts'

Can somebody please help me?

4
  • 1
    The documentation mentions this function for a Series. If you want to get it for multiple columns, use apply. Some discussions about Commented Aug 4, 2019 at 18:23
  • @Trenton_M Hi! does this solution work for specific columns in all the excel sheets in the workbook? Commented Aug 4, 2019 at 19:20
  • replace df2.value_counts() with df2.apply(pd.Series.value_counts) inside the loop Commented Aug 4, 2019 at 19:27
  • @Trenton_M tks man! Commented Aug 4, 2019 at 19:42

1 Answer 1

0

You have a loop running on each sheet in the source Excel file. It is OK, but you should apply value_counts separately to each DataFrame (read from each sheet).

And a remark concerning variable names: Use df for DataFrames only. Neither Excel file nor sheet name are DataFrames.

So the loop should be somenthing like:

exFile = pd.ExcelFile("filepath.xlsx")
for sheet in exFile.sheet_names:
    df = pd.read_excel("filepath.xlsx", sheet_name=sheet, usecols="F")
    df.dropna(inplace=True)
    print('Sheet', sheet)
    print(df.apply(pd.Series.value_counts))
Sign up to request clarification or add additional context in comments.

1 Comment

Omg I was under the impression df was needed for all pd for like the entire wk LOL

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.