我有一个简单的数字数据框,我想找到它的数字根,也作为一个数据框
我用来查找数字根的代码是
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
关于如何执行此任务有什么建议吗?
谢谢
最佳答案
您的 digital_root 需要一个 int。
你给它一个字符串
df = a.to_string()
x = digital_root(df)
您不打算发送 a = w_24.iloc[w, i]
吗?注意 iloc[w, i]
不是 iloc[w:i]
,2 轴索引不是第一个轴的切片
关于python - 查找数字数据帧的数字根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54584805/