Search code examples
pandasmultiple-columnsfrequency

frequency table for all columns in pandas


I want to run frequency table on each of my variable in my df.

def frequency_table(x):
    return pd.crosstab(index=x,  columns="count")

for column in df:
    return frequency_table(column)

I got an error of 'ValueError: If using all scalar values, you must pass an index'

How can i fix this?

Thank you!


Solution

  • You aren't passing any data. You are just passing a column name.

    for column in df:
        print(column) # will print column names as strings
    

    try

    ctabs = {}
    for column in df:
        ctabs[column]=frequency_table(df[column])
    

    then you can look at each crosstab by using the column name as keys in the ctabs dictionary