Search code examples
pythonpandasvalueerror

Finding the digital root of a dataframe of numbers


i have a simple dataframe of numbers and I want to find its digital root, also as a data frame

the code I am using for finding the digital root is

def digital_root (n):
    ap = 0
    n = abs(int(n))
    while n >= 10:
        n = sum(int(digit) for digit in str(n))
        ap += 1
    return n

and the code I am writing is

for i in range(0, 24):
    for w in range(0, len(w_24)):
        column = []
        a = w_24.iloc[w:i]
        df = a.to_string()
        x = digital_root(df)
        column.append(x)
    w_dr = pd.DataFrame(list(column.items()), columns = ['i']) 

I am getting the following error:

ValueError: invalid literal for int() with base 10: 'Empty DataFrame\nColumns

any suggestions on how to perform this task?

thanks


Solution

  • Your digital_root is expecting an int.

    You give it a string

    df = a.to_string()
    x = digital_root(df)
    

    Did you not intend to send a = w_24.iloc[w, i] instead? Note iloc[w, i] not iloc[w:i], a 2 axis index not a slice of the first axis