我正試圖為每一組4個行/值按列追加行。 我有11個值,前4個值應該在一個串聯的行中,第5行到第8行是一個值,最后3行是一個值,即使分割的值不是4個。
df_in = pd. DataFrame({'Column_IN': ['text 1','text 2', 'text 3', 'text 4','text 5','text 6', 'text 7','text 8','text 9','text 10','text 11']})
而我的預期輸出如下
df_out = pd. DataFrame({'Column_OUT': ['text 1&text 2&text 3&text 4','text 5& text 6& text 7& text 8','text 9& text 10& text 11']})
我試圖得到我想要的輸出df_out,如下所示。
df_2 = df_1.iloc[:-7].agg('&'.join).to_frame()
為了獲得所需的輸出,需要進行任何修改嗎?
uj5u.com熱心網友回復:
嘗試使用groupby和agg:
>>> df_in.groupby(df_in.index // 4).agg('& ' /span>.join)
列_IN
0 text 1&text 2&text 3&text 4.
1 text 5&text 6& text 7&text 82 text 9& text 10& text 11
>>>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/324013.html
標籤:
