简体   繁体   English

根据class属性向DataFrame动态添加行

[英]Add row dynamically based on the class attribute to a DataFrame

I am trying to dynamically add a line to my pandas.DataFrame based on the class attributes, but it does not work for some reason. 我试图基于类属性向我的pandas.DataFrame动态添加一行,但是由于某些原因它无法正常工作。 Hopefully an example makes more sense: 希望一个例子更有意义:

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)

This outputs same move twice, while I want to save the first move and the second move in a seperate rows. 这将输出两次相同的动作,而我想将第一步和第二步保存在单独的行中。 Every time a move is played I want to save the new position in a row in match1.moves and the last position in match1.position. 每次播放移动时,我想将新位置保存在match1.moves中的行中,并将最后一个位置保存在match1.position中。

There were a couple of issues with your implementation. 您的实现存在两个问题。

  1. If you want a DataFrame, self.move should not be a list 如果要使用DataFrame,则self.move不应为列表
  2. If you want unique board snapshots in each row, then you need to copy the board every time you save it. 如果您希望每一行都有唯一的板快照,那么每次保存时都需要复制该板。

Code: 码:

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

Test Code: 测试代码:

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)

Results: 结果:

[[ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据条件向数据框添加一行 - Add a row to a dataframe based on a condition 使用Python在运行时动态地向类添加属性 - Add an attribute to a class dynamically at runtime in Python 根据其他值将数据框中的值添加到行 - Add value in dataframe to row based on other value 根据特定条件在 dataframe 行中添加值 - Add values in a dataframe row based on a specific condition 根据来自熊猫数据帧的行数动态添加字典值 - Dynamically adding dictionary values based on row count from pandas dataframe 如何动态划分数据帧(基于行中的第一次出现) - How to divide a dataframe dynamically (based on first ocurrence in the row) 如何根据行的“属性”将数据帧行乘以数组? - How to multiply dataframe rows by an array based on an 'attribute' of the row? 如何根据行的“属性”将 dataframe 行乘以数组,同时保留与数组属性不匹配的 dataframe 行? - How to multiply dataframe rows by an array based on an 'attribute' of the row whilst preserving dataframe rows not matching array attribute? 根据每行上一行中的值更新pandas dataframe当前行属性 - Update pandas dataframe current row attribute based on its value in the previous row for each row 如何为动态分配给类的属性添加类型注释? - How can I add type annotation for the attribute that is dynamically assigned to a class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM