使用列表时熊猫数据框替换方法不起作用

时间:2018-11-14 16:26:13

标签: python pandas dataframe replace

我要转换此数据框:

    col1    col2
0   [1, 0]  [0, 1]
1   [1, 1]  [1, 1]
2   [0, 1]  [1, 0]
3   [0, 1]  [1, 1]
4   [1, 1]  [0, 1]
5   [1, 0]  [1, 0]

进入这个:

    col1    col2
0   [1, 0]  [0, 1]
1        1       1
2   [0, 1]  [1, 0]
3   [0, 1]       1
4        1  [0, 1]
5   [1, 0]  [1, 0]

我尝试使用replace

df.replace([1,1], 1)

但这没用。

1 个答案:

答案 0 :(得分:2)

使用applymap

df.applymap(lambda x :  1 if x==[1,1] else x )
Out[162]: 
     col1    col2
0  [1, 0]  [0, 1]
1       1       1