3

Rather than explicitly specifying the DataFrame columns in the code below, I'm trying to give an option of passing the name of the data frame in itself, without much success.

The code below gives a

"ValueError: Wrong number of dimensions" error.

I've tried another couple of ideas but they all lead to errors of one form or another.

Apart from this issue, when the parameters are passed as explicit DataFrame columns, p as a single column, and q as a list of columns, the code works as desired. Is there a clever (or indeed any) way of passing in the data frame so the columns can be assigned to it implicitly?

def cdf(p, q=[], datafr=None):
    if datafr!=None:
        p = datafr[p]
        for i in range(len(q)):
            q[i]=datafr[q[i]]
...
(calculate conditional probability tables for p|q) 

to summarize:

current usage:

cdf(df['var1'], [df['var2'], df['var3']])

desired usage:

cdf('var1', ['var2', 'var3'], datafr=df)

1 Answer 1

11

Change if datafr != None: to if datafr is not None:

Pandas doesn't know which value in the dataframe you are trying to compare to None so it throws an error. is checks if both datafr and None are the pointing to the same object, which is a more stringent identity check. See this explanation.

Additional tips:

  • Python iterates over lists

    #change this 
    for i in range(len(q)):
          q[i]=datafr[q[i]]
    #to this:
    for i in q:
       q[i] = datafr[q]
    
  • If q is a required parameter don't do q = [ ] when defining your function. If it is an optional parameter, ignore me.

  • Python can use position to match the arguments passed to the function call to with the parameters in the definition.

    cdf('var1', ['var2', 'var3'], datafr=df)
    #can be written as:
    cdf('var1', ['var2', 'var3'], df)
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. The datafr is not None did the trick. I had already tried the list iteration for the q[i]'s, and it threw an error, OTOH the code I had seems to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.