如何使用切片或其他方法在熊猫内分配列表值?

时间:2019-01-26 16:37:17

标签: python-3.x pandas

我有一个“矩阵”列,并且有一个名为“ after_class”的行的列表,我想将after_class中第二行元素的每一行的值赋值为0, 行数:after_class = [2、10、14、17、20、27、29、33]。但是,正如我尝试过的那样,df.loc [1,'Matrix']也变为0,为什么会这样?

print(df.head(11))            
print(after_class)
for n in after_class:

   df.loc[n, 'Matrix'][1] = 0
   print(n, df.loc[n, 'Matrix'])
print(df.head(11))
                   time   open   high   ...     K_Class  diff   Matrix
0   2019-01-23 18:25:00  2.420  2.421   ...          -1   NaN      NaN
1   2019-01-23 18:35:00  2.417  2.418   ...           1   2.0  [-1, 1]
2   2019-01-23 18:40:00  2.418  2.419   ...           1   0.0  [-1, 1]
3   2019-01-23 18:50:00  2.422  2.423   ...           1   0.0      NaN
4   2019-01-23 18:55:00  2.421  2.430   ...           1   0.0      NaN
5   2019-01-23 19:10:00  2.426  2.432   ...           1   0.0      NaN
6   2019-01-23 19:15:00  2.431  2.437   ...           1   0.0      NaN
7   2019-01-23 19:20:00  2.436  2.447   ...           1   0.0      NaN
8   2019-01-23 19:25:00  2.443  2.448   ...           1   0.0      NaN
9   2019-01-23 19:30:00  2.447  2.458   ...          -1  -2.0   [1, 1]
10  2019-01-23 19:40:00  2.448  2.450   ...          -1   0.0   [1, 1]


[2, 10, 14, 17, 20, 27, 29, 33]


0   2019-01-23 18:25:00  2.420  2.421   ...      NaN      NaN      NaN
1   2019-01-23 18:35:00  2.417  2.418   ...      2.0  [-1, 0]  [-1, 0]
2   2019-01-23 18:40:00  2.418  2.419   ...      0.0  [-1, 0]  [-1, 0]
3   2019-01-23 18:50:00  2.422  2.423   ...      0.0      NaN      NaN
4   2019-01-23 18:55:00  2.421  2.430   ...      0.0      NaN      NaN
5   2019-01-23 19:10:00  2.426  2.432   ...      0.0      NaN      NaN
6   2019-01-23 19:15:00  2.431  2.437   ...      0.0      NaN      NaN
7   2019-01-23 19:20:00  2.436  2.447   ...      0.0      NaN      NaN
8   2019-01-23 19:25:00  2.443  2.448   ...      0.0      NaN      NaN
9   2019-01-23 19:30:00  2.447  2.458   ...     -2.0   [1, 0]   [1, 0]
10  2019-01-23 19:40:00  2.448  2.450   ...      0.0   [1, 0]   [1, 0]   

1 个答案:

答案 0 :(得分:0)

问题可能是第1行矩阵和第2行矩阵列元素是同一对象。第10行和第9行元素也可能是同一情况,因为两者相同。
如示例所示

import pandas as pd

data = [[[1,2]]]*3
row = [0,2]
print('row',row)
df = pd.DataFrame(data = data, columns=["Row-data"])

print(df)

for r in row:   
    df.loc[r,"Row-data"][1] = 0

print(df)

输出

row [0, 2]
  Row-data
0   [1, 2]
1   [1, 2]
2   [1, 2]
  Row-data
0   [1, 0]
1   [1, 0]
2   [1, 0]

Row数据中的所有元素都已更改,因为每一行中的对象都相同。因此,如果您在对象的任何行中进行任何更改,它都会反映在放置了相同对象的其他行中。

为了将它们视为不同的对象(解决方案)

您可以使用如下所示的复制库

import pandas as pd
import copy

data = [[[1,2]]]*3
row = [0,2]
print('row',row)
df = pd.DataFrame(data = data, columns=["Row-data"])

print(df)

for r in row:
    # getting the copy of object    
    row_data = copy.deepcopy(df.loc[r,"Row-data"])
    # modifying the copy
    row_data[1] = 0
    # assigning the copy to that row
    df.loc[r,"Row-data"] = row_data

print(df)

输出

row [0, 2]
  Row-data
0   [1, 2]
1   [1, 2]
2   [1, 2]
  Row-data
0   [1, 0]
1   [1, 2]
2   [1, 0]

我希望这可以解决您的问题:D