小编rea*_*ind的帖子

Pandas DataFrame 的组合与子类

我正在尝试在 Python 类中创建一个名为“tilt”的方法,该方法使用 Pandas 将 DataFrame 颠倒过来。但每当我在此类创建的实例上使用此“倾斜”方法时,我都会收到此错误“该对象没有属性“iloc””。

import numpy as np
import pandas as pd

class Board():

    def __init__(self):
        pass

    def arrange(self):

        board=pd.DataFrame(np.arange(1,65).reshape(8,8),index=[1,2,3,4,5,6,7,8],columns=[1,2,3,4,5,6,7,8])
        self = board.copy()
        self.loc[1]=['BP1','BP2','BP3','BP4','BP5','BP6','BP7','BP8']
        self.loc[2]=['blR','blK','blB','bQ','bK','brB','brK','brR']
        self.loc[7]=['wlR','wlK','wlB','wK','wQ','wrB','wrK','wrR']
        self.loc[8]=['WP1','WP2','WP3','WP4','WP5','WP6','WP7','WP8']
        print(self)

    def tilt(self):
        self.iloc[::-1]
        print(self)
Run Code Online (Sandbox Code Playgroud)

python class pandas

2
推荐指数
1
解决办法
750
查看次数

标签 统计

class ×1

pandas ×1

python ×1