我想用一个名为chess_dict的字典将一个包含国际象棋棋盘坐标的Pandas数据帧df的三列替换为形式为2x1整数列表的笛卡尔坐标。我尝试使用replace方法,但由于某些原因它只替换了列表的第一个整数,而不是两个整数。例如:('a1','h6') -> ([0,0],[7,5])。
>>>chess_dict
{'a1': [0, 0],
'a2': [0, 1],
...
'h5': [7, 4],
'h6': [7, 5],
'h7': [7, 6],
'h8': [7, 7]}
>>>df
WK WR BK plays outcome
0 h4 e6 a4 b w
1 a6 h4 g8 w w
2 f5 a3 d1 w w
3 c2 h3 f5 w w
...
>>>df.ix[:,0].replace(chess_dict, inplace=True)
>>>df.ix[:,1].replace(chess_dict, inplace=True)
>>>df.ix[:,2].replace(chess_dict, inplace=True)
>>>df
WK WR BK plays outcome
0 7 4 0 b w
1 5 3 7 w w
2 5 0 3 w w
3 1 2 4 w w
...
I have tried replacing with the Cartesian coords as strings, and afterwards converting the string to integer lists and it worked, but since I'm new to python and pandas I'm guessing there is a simpler way using replace but there is something basic I'm missing which I just can't seem to figure out.
I've also tried this syntax with the same results:
df.ix[:,0].replace(to_replace = chess_dict.keys(), value = chess_dict.values(), inplace=True)
I've also tried using integer tuples for the Cartesian coords (0,0), (0,1), etc., but now only the last instead of first value is inserted in the DataFrame column
WK WR BK plays outcome 0 3 g7 b4 b w 1 3 h7 d1 b w 2 3 h6 f5 w w 3 6 d5 b1 b w