我有一个包含3列的DF,我想执行布尔检查。代码如下所示。
import pandas as pd
df = pd.DataFrame({
'Col1':[A,A,A,B,B,C,C,C],
'Col2':[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],[1, 2, 3, 4, 5],[1, 2, 3, 4, 5], [1, 2, 3, 4, 5,6,7] ,[1, 2, 3, 4, 5,6,7],[1, 2, 3, 4, 5,6,7] ]
'Col3': [[1, 2, 3, 4],[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],[1, 2, 3, 4, 5, 6, 7, 8, 9],[1, 2, 3, 4, 5],[1, 2, 3], [1, 2, 3, 4, 5,6,7] ,[1, 2, 3],[1, 2, 3, 4] ]
})
我想要一个给我DF1唯一值的DF,如果Col2,Col3的一行相等,则打印True,否则打印False。我想看到的结果是:
'Col1' 'Col2' 'Col3' 'Col4'
A [1,2,3,4,5,6,7,8,9,10,11,12] [1,2,3,4] False
print(map(type, df['Col2']) == map(type,df['Col3']))
这使我返回true值,但我希望它作为df中的新列Col4
对于我尝试的多个代码,我总是发现错误为unhashable type: 'list'
答案 0 :(得分:0)
转换为字符串以进行比较:
df['Col4'] = df.Col2.map(str) == df.Col3.map(str)
答案 1 :(得分:0)
如果要对每组Col1
应用此逻辑,请使用groupby.apply
:
df['Col4'] = df.groupby('Col1').apply(lambda x: x['Col2'].eq(x['Col3'])).values
Col1 Col2 Col3 Col4
0 A [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [1, 2, 3, 4] False
1 A [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] True
2 A [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [1, 2, 3, 4, 5, 6, 7, 8, 9] False
3 B [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] True
4 B [1, 2, 3, 4, 5] [1, 2, 3] False
5 C [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7] True
6 C [1, 2, 3, 4, 5, 6, 7] [1, 2, 3] False
7 C [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4] False
不考虑组,在这种情况下,使用eq
df['Col4'] = df['Col2'].eq(df['Col3'])
Col1 Col2 Col3 Col4
0 A [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [1, 2, 3, 4] False
1 A [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] True
2 A [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [1, 2, 3, 4, 5, 6, 7, 8, 9] False
3 B [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] True
4 B [1, 2, 3, 4, 5] [1, 2, 3] False
5 C [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7] True
6 C [1, 2, 3, 4, 5, 6, 7] [1, 2, 3] False
7 C [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4] False
答案 2 :(得分:0)
引入一种新方法tuple
df.Col2.map(tuple)==df.Col3.map(tuple)
Out[646]:
0 False
1 True
2 False
3 True
4 False
5 True
6 False
7 False
dtype: bool