2016-08-26 2 views
3

CSVファイルを取得し、行を分割してカスケードする必要があります。入力CSVはさまざまな量の列を持つことができます(常に偶数)が、同じ方法で常に分割されます。私はいくつかのファイルで出力が50万行になるので、それをスピードアップすると思ったので、Pandasを使うことにしました。データフレーム内の行を動的に分割する

入力:

h1 h2 h3 h4 h5 h6 
A1 A2 A3 A4 A5 A6 
B1 B2 B3 B4 B5 B6 

予想される出力

h1 h2 h3 h4 h5 h6 
A1 A2 
A1 A2 A3 A4 
A1 A2 A3 A4 A5 A6 
B1 B2 
B1 B2 B3 B4 
B1 B2 B3 B4 B5 B6 

私はあなたがそれが近い見ることができるように(いくつかの検索と自分の編集から一緒に石畳)以下のコードを使用してみました、ではなく、かなり私は何が必要です。与え

importFile = pd.read_csv('file.csv') 
df = df_importFile = pd.DataFrame(importFile) 

index_cols = ['h1'] 
cols = [c for c in df if c not in index_cols] 

df2 = df.set_index(index_cols).stack().reset_index(level=1, drop=True).to_frame('Value') 

df2 = pd.concat([pd.Series([v if i % len(cols) == n else '' 
         for i, v in enumerate(df2.Value)], name=col) 
      for n, col in enumerate(cols)], axis=1).set_index(df2.index) 


df2.to_csv('output.csv') 

h1 h2 h3 h4 h5 h6 
A1 A2 
A1  A3 
A1   A4 
A1    A5 
A1     A6 

答えて

3
# take number of columns and divide by 2 
# this is the number of pairs 
pairs = df.shape[1] // 2 

# np.repeat takes the number of rows and returns an object to slice 
# the dataframe array df.values... then slice... result should be 
# of length pairs * len(df) 
a = df.values[np.repeat(np.arange(df.shape[0]), pairs)] 

# row values to condition with as column vector 
dim0 = (np.arange(a.shape[0]) % (pairs))[:, None ] 

# column values to condition with as row vector 
dim1 = np.repeat(np.arange(pairs), 2) 

# boolean mask to use in np.where generated 
# via the magic of numpy broadcasting 
mask = dim0 >= dim1 

# QED 
pd.DataFrame(np.where(mask, a, ''), columns=df.columns) 

enter image description here

+0

。入力ファイルのすべての行をロールバックする必要があります。 – mrh5028

+0

@ mrh5028 unftated – piRSquared

+0

完璧に作業しました!今、何が起こっているのかを正確に理解するために作業します。 – mrh5028

3

はこれを試してみてください。これは動作しますが、最初の2つの行に当たる

dfNew = pd.DataFrame() 
ct = 1 
while ct <= df.shape[1]/2 : 
    dfNew = dfNew.append(df[df.columns[:2*ct]]) 
    ct +=1 

dfNew.sort_values(['h1'], ascending=[True]).reset_index(drop=True).fillna("") 
print df 

    h1 h2 h3 h4 h5 h6 
0 A1 A2     
1 A1 A2 A3 A4   
2 A1 A2 A3 A4 A5 A6 
3 B1 B2     
4 B1 B2 B3 B4   
5 B1 B2 B3 B4 B5 B6 
関連する問題