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. Hopefully an example makes more sense:
我试图根据类属性动态地向我的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)
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中保存最后一个位置。
1 个解决方案
#1
1
There were a couple of issues with your implementation.
您的实施存在一些问题。
- If you want a DataFrame, self.move should not be a list
- If you want unique board snapshots in each row, then you need to copy the board every time you save it.
如果你想要一个DataFrame,self.move不应该是一个列表
如果您想在每行中创建唯一的板快照,则每次保存时都需要复制板。
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
#1
1
There were a couple of issues with your implementation.
您的实施存在一些问题。
- If you want a DataFrame, self.move should not be a list
- If you want unique board snapshots in each row, then you need to copy the board every time you save it.
如果你想要一个DataFrame,self.move不应该是一个列表
如果您想在每行中创建唯一的板快照,则每次保存时都需要复制板。
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