在python中,如何将数据帧的所有值连接成整数?
我有以下数据帧:在python中,如何将数据帧的所有值连接成整数?,python,pandas,dataframe,Python,Pandas,Dataframe,我有以下数据帧: 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 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
有没有人知道用尽可能短的代码片段来做这件事的方法。我感谢你的建议。谢谢。IIUC
''.join(str(x) for x in sum(df.values.tolist(),[]))
Out[344]: '110111001011111100111111110011111111011111111100111111111011'
或
IIUC
或
选项1
apply(str.join)
+str.cat
:
df.astype(str).apply(''.join, 1).str.cat(sep='')
'110111001011111100111111110011111111011111111100111111111011'
选项2
应用
+np.添加
,由温提出:
np.sum(df.astype(str).apply(np.sum, 1))
'110111001011111100111111110011111111011111111100111111111011'
选项1
apply(str.join)
+str.cat
:
df.astype(str).apply(''.join, 1).str.cat(sep='')
'110111001011111100111111110011111111011111111100111111111011'
选项2
应用
+np.添加
,由温提出:
np.sum(df.astype(str).apply(np.sum, 1))
'110111001011111100111111110011111111011111111100111111111011'
什么IIUC?在你的答案文本中哦,是的,这正是我想要的。@Elisha512对不起,我的答案中有一个拼写错误,我已经纠正了。检查我们的两个解决方案,看看哪一个对您的数据更快。什么IIUC?在你的答案文本中哦,是的,这正是我想要的。@Elisha512对不起,我的答案中有一个拼写错误,我已经纠正了。检查我们的两个解决方案,看看哪一个对您的数据更快。也许我们可以使用sum
np.sum(df.astype(str).apply(np.sum,1))
也许我们可以使用sumnp.sum(df.astype(str).apply(np.sum,1))