簡體   English   中英

如何在python中將pandas數據幀的所有值連接為整數?

[英]How to concatenate all values of a pandas dataframe into an integer in python?

我有以下數據框:

           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

然后獲取,然后將值連接到一個long int數中,這樣它就變成了:

110111001011111100111111110011111111011111111100111111111011

有誰知道在最短的代碼段中完成此操作的方法。 我感謝這些建議。 謝謝。

IIUC

''.join(str(x) for x in sum(df.values.tolist(),[]))
Out[344]: '110111001011111100111111110011111111011111111100111111111011'

要么

''.join(map(str,sum(df.values.tolist(),[])))

選項1
apply(str.join) + str.cat

df.astype(str).apply(''.join, 1).str.cat(sep='')
'110111001011111100111111110011111111011111111100111111111011'

選項2
apply + np.add ,由Wen提出:

np.sum(df.astype(str).apply(np.sum, 1))
'110111001011111100111111110011111111011111111100111111111011'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM