2017-10-05 84 views
0

我在Python字典中收集了兩個數據框。每個數據框都有一個由0和1組合組成的字符串列。此外,字符串的長度隨着長度是該月中的天數而變化。Python:將數據幀集合中的列拆分爲單個數字列

我的問題是,我無法弄清楚如何將字符串列拆分爲許多,以便在每列中只有一個或零或缺少的值。

我已經看到線程的建議,可以通過使用list(map(int(i) for i in str(01111001))將單個數字拆分爲其數字。

但是,我怎麼能把下面的字典中的col假日分成許多列,以便每個列只包含一個或零或缺少的值,如果特定記錄較短。

'ATM': 
    Plant   Year Month Holiday 
    01    1996 Mar '01111001' 
    02    1997 Feb '0111011' 
    SP    1996 Mar '01100111' 
    BE    1999 Mar '00111111' 

'FDA': 
Plant   Year  Month Holiday 
    01    2001 Mar '01111101' 
    02    2002 Mar '11110110' 
    SP    2001 Apr '1110011' 
    BE    2002 June '10111100' 

我想實現看起來如下結果:

'ATM': 
    Plant   Year Month H1 H2 H3 H4 H5 H6 H7 H8 
    01    1996 Mar 0 1 1 1 1 0 0 1 
    02    1997 Feb 0 1 1 1 0 1 1 NA 
    SP    1996 Mar 0 1 1 0 0 1 1 1 
    BE    1999 Mar 0 0 1 1 1 1 1 1 

'FDA': 
Plant   Year  Month H1 H2 H3 H4 H5 H6 H7 H8 
    01    2001 Mar 0 1 1 1 1 1 0 1 
    02    2002 Mar 1 1 1 1 0 1 1 0 
    SP    2001 Apr 1 1 1 0 0 1 1 NA 
    BE    2002 June 1 0 1 1 1 1 0 0 
+0

很抱歉,但我並不清楚你的要求。特別是你有什麼和你想要達到什麼。你可以擴展一點,也許你的代碼的一部分? –

+0

我想將列Holiday分成許多列,每列只有一個「1」或「0」元素。 –

+0

'01111001'是一個無效的語法。你有'0b01111001'嗎?那麼這只是一個用不同基數打印的整數。或者你有'01111001''這已經是一個字符串了? –

回答

1

我創建了一個打印出你的願望一個小的測試代碼。這個想法是使用strnumpy矩陣來存儲這些值。矩陣充滿了「NA」,因此它們將在最後出現。比伎倆使用廣播複製在所需的地方值。整個數據框通過串聯和刪除不需要的列完成。代碼遍歷字典的鍵。我假設您使用的是pandas數據框,並且加載的二進制值被解釋爲object s。

代碼的第一部分是構造數據框字典的標頭。

import pandas as pd 
import numpy as np 

## Lets call it "header" 

from io import StringIO 

df_0 = """ 
Plant;Year;Month;Holiday 
01;1996;Mar;01111001 
02;1997;Feb;0111011 
SP;1996;Mar;01100111 
BE;1999;Mar;00111111 
""" 

df_1 = """ 
Plant;Year;Month;Holiday 
01;2001;Mar;01111101 
02;2002;Mar;11110110 
SP;2001;Apr;1110011 
BE;2002;June;10111100 
""" 

df_0 = pd.read_csv(StringIO(df_0), sep=";", dtype=object); 
df_1 = pd.read_csv(StringIO(df_1), sep=";", dtype=object); 

df = { "ATM": df_0, "PDE": df_1 } 

## "Header" end 

MAX_SIZE = 8 

for k in df: 
    ldf = df[k] 
    rows = ldf.shape[1] 

    # Here I create a matrix that will contain my required values "NA" 
    nmat = np.full((rows, MAX_SIZE), "NA") 

    for i in range(rows): 
     # I'm using the same conversion that I suggested you in 
     # the comments 
     ary = np.array([v for v in ldf["Holiday"][i]]) 
     # Copying only the needed part, in some cases the final 
     # array is of len 7 instead of 8. 
     nmat[i, 0:len(ary)] = ary 

    # Creating a new dataframe that will be 
    # concatenated by using the numpy array generated before. 
    nframe = pd.DataFrame(nmat, 
      columns=["H" + str(i+1) for i in range(MAX_SIZE)]) 
    # Actual concatenation 
    ldf = pd.concat([ldf, nframe], axis=1) 
    # and deletion on "Holiday" columns 
    del ldf["Holiday"] # only if really needed, removes Holiday column 
    # Substitution in the original array 
    df[k] = ldf 

# et voillà 
print(df) 

它吐出:

{ 
    'ATM': 
    Plant Year Month H1 H2 H3 H4 H5 H6 H7 H8 
    0 01 1996 Mar 0 1 1 1 1 0 0 1 
    1 02 1997 Feb 0 1 1 1 0 1 1 NA 
    2 SP 1996 Mar 0 1 1 0 0 1 1 1 
    3 BE 1999 Mar 0 0 1 1 1 1 1 1, 
    'PDE': 
    Plant Year Month H1 H2 H3 H4 H5 H6 H7 H8 
    0 01 2001 Mar 0 1 1 1 1 1 0 1 
    1 02 2002 Mar 1 1 1 1 0 1 1 0 
    2 SP 2001 Apr 1 1 1 0 0 1 1 NA 
    3 BE 2002 June 1 0 1 1 1 1 0 0 
} 
+0

哇!感謝您接受答案! –