Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于类属性向数据帧动态添加行_Python_Class_Pandas_Object_Numpy - Fatal编程技术网

Python 基于类属性向数据帧动态添加行

Python 基于类属性向数据帧动态添加行,python,class,pandas,object,numpy,Python,Class,Pandas,Object,Numpy,我正在尝试根据类属性动态地向pandas.DataFrame添加一行,但由于某些原因,它不起作用。希望一个例子更有意义: import numpy as np import pandas as pd class match: def __init__(self): self.position = np.zeros(shape = (3, 3)) self.moves = [] def PlayMove(self, x_coordinate, y_coordinate, pla

我正在尝试根据类属性动态地向pandas.DataFrame添加一行,但由于某些原因,它不起作用。希望一个例子更有意义:

import numpy as np
import pandas as pd

class match:
def __init__(self):
    self.position = np.zeros(shape = (3, 3))
    self.moves = []

def PlayMove(self, x_coordinate, y_coordinate, player_name):
    if player_name == "player1":
        self.position[x_coordinate, y_coordinate] = 1
    if player_name == "player2":
        self.position[x_coordinate, y_coordinate] = 4
    self.moves.append(pd.DataFrame(self.position.reshape(1, 9)))

match1 = match()
match1.PlayMove(1,2,"player1")
print(match1.position)
print(match1.moves)
match1.PlayMove(2,2,"player1")
print(match1.position)
print(match1.moves)

这将输出两次相同的移动,而我希望将第一次移动和第二次移动保存在单独的行中。每次播放移动时,我都希望将新位置保存在match1.moves中的一行中,并将最后一个位置保存在match1.position中。

您的实现存在一些问题

  • 如果需要数据帧,self.move不应是列表
  • 如果要在每行中创建唯一的线路板快照,则需要在每次保存线路板时复制该线路板
  • 代码:

    class match:
        def __init__(self):
            self.position = np.zeros(shape=(3, 3))
            self.moves = None
    
        def PlayMove(self, x_coordinate, y_coordinate, player_name):
            if player_name == "player1":
                self.position[x_coordinate, y_coordinate] = 1
            else:
                self.position[x_coordinate, y_coordinate] = 4
            move = pd.DataFrame(np.array(self.position).reshape(1, 9))
            self.moves = pd.concat([self.moves, move])
    
    match1 = match()
    match1.PlayMove(1, 2, "player1")
    print(match1.position)
    print('\n1:\n', match1.moves)
    match1.PlayMove(2, 2, "player2")
    print('\n', match1.position)
    print('\n2:\n', match1.moves)
    
    [[ 0.  0.  0.]
     [ 0.  0.  1.]
     [ 0.  0.  0.]]
    
    1:
         0    1    2    3    4    5    6    7    8
    0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0
    
    [[ 0.  0.  0.]
     [ 0.  0.  1.]
     [ 0.  0.  4.]]
    
    2:
         0    1    2    3    4    5    6    7    8
    0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0
    0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  4.0
    
    测试代码:

    class match:
        def __init__(self):
            self.position = np.zeros(shape=(3, 3))
            self.moves = None
    
        def PlayMove(self, x_coordinate, y_coordinate, player_name):
            if player_name == "player1":
                self.position[x_coordinate, y_coordinate] = 1
            else:
                self.position[x_coordinate, y_coordinate] = 4
            move = pd.DataFrame(np.array(self.position).reshape(1, 9))
            self.moves = pd.concat([self.moves, move])
    
    match1 = match()
    match1.PlayMove(1, 2, "player1")
    print(match1.position)
    print('\n1:\n', match1.moves)
    match1.PlayMove(2, 2, "player2")
    print('\n', match1.position)
    print('\n2:\n', match1.moves)
    
    [[ 0.  0.  0.]
     [ 0.  0.  1.]
     [ 0.  0.  0.]]
    
    1:
         0    1    2    3    4    5    6    7    8
    0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0
    
    [[ 0.  0.  0.]
     [ 0.  0.  1.]
     [ 0.  0.  4.]]
    
    2:
         0    1    2    3    4    5    6    7    8
    0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0
    0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  4.0
    
    结果:

    class match:
        def __init__(self):
            self.position = np.zeros(shape=(3, 3))
            self.moves = None
    
        def PlayMove(self, x_coordinate, y_coordinate, player_name):
            if player_name == "player1":
                self.position[x_coordinate, y_coordinate] = 1
            else:
                self.position[x_coordinate, y_coordinate] = 4
            move = pd.DataFrame(np.array(self.position).reshape(1, 9))
            self.moves = pd.concat([self.moves, move])
    
    match1 = match()
    match1.PlayMove(1, 2, "player1")
    print(match1.position)
    print('\n1:\n', match1.moves)
    match1.PlayMove(2, 2, "player2")
    print('\n', match1.position)
    print('\n2:\n', match1.moves)
    
    [[ 0.  0.  0.]
     [ 0.  0.  1.]
     [ 0.  0.  0.]]
    
    1:
         0    1    2    3    4    5    6    7    8
    0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0
    
    [[ 0.  0.  0.]
     [ 0.  0.  1.]
     [ 0.  0.  4.]]
    
    2:
         0    1    2    3    4    5    6    7    8
    0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0
    0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  4.0