我有以下数据框:
1 2 3 4 5 6 7 8 9 10
dog cat 1 1 0 1 1 1 0 0 1 0
dog 1 1 1 1 1 1 0 0 1 1
fox 1 1 1 1 1 1 0 0 1 1
jumps 1 1 1 1 1 1 0 1 1 1
over 1 1 1 1 1 1 0 0 1 1
the 1 1 1 1 1 1 1 0 1 1
我想首先从行和列中删除所有标签,以便df变为:
1 1 0 1 1 1 0 0 1 0
1 1 1 1 1 1 0 0 1 1
1 1 1 1 1 1 0 0 1 1
1 1 1 1 1 1 0 1 1 1
1 1 1 1 1 1 0 0 1 1
1 1 1 1 1 1 1 0 1 1
然后将值连接成一个长整数,使其变为:
110111001011111100111111110011111111011111111100111111111011
有没有人知道在最短的代码片段中做到这一点的方法。我很欣赏这些建议。谢谢。
答案 0 :(得分:2)
IIUC
''.join(str(x) for x in sum(df.values.tolist(),[]))
Out[344]: '110111001011111100111111110011111111011111111100111111111011'
或
''.join(map(str,sum(df.values.tolist(),[])))
答案 1 :(得分:2)
选项1
apply(str.join)
+ str.cat
:
df.astype(str).apply(''.join, 1).str.cat(sep='')
'110111001011111100111111110011111111011111111100111111111011'
选项2
Wen提出的apply
+ np.add
:
np.sum(df.astype(str).apply(np.sum, 1))
'110111001011111100111111110011111111011111111100111111111011'