发现号的数据框的数字根

问题描述 投票:1回答:1

我有一个数字的简单数据框,我想找到它的数字根,也可以作为一个数据帧

我使用的代码寻找数字根源

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

和我写的代码是

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']) 

我收到以下错误:

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

关于如何执行此任务有什么建议?

谢谢

python pandas valueerror
1个回答
0
投票

你digital_root期待一个int。

你给它一个字符串

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

你不是打算发送a = w_24.iloc[w, i]呢?注意iloc[w, i]iloc[w:i],所述第一轴的轴2不索引片

© www.soinside.com 2019 - 2024. All rights reserved.